import { IsString, IsOptional, IsBoolean, IsNotEmpty, MaxLength, IsUUID } from 'class-validator';
import { CampaignStatus } from '../enum/pes.enum';
import { PartialType } from '@nestjs/mapped-types';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { PesCampaign } from '../entities/pes-campaign.entity';
import { PesVerifiedUserResponseDto } from './pes-verified-users.dto';
import { PesTemplateResponseDto } from './pes-template.dto';
import { PesSendingProfileResponseDto } from './pes-sending-profile.dto';
import { PesLandingPageResponseDto } from './pes-landing-page.dto';

export class CreatePesCampaignDto {
  @ApiProperty({ example: 'Summer Promo Campaign', description: 'Name of the campaign' })
  @IsString()
  @IsNotEmpty()
  name: string;

  @ApiPropertyOptional({ example: 'Promotional campaign for summer season', description: 'Optional campaign description' })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  description?: string;

  @ApiProperty({ description: 'ID of the associated template', example: '' })
  @IsString()
  @IsNotEmpty()
  @MaxLength(36)
  @IsUUID()
  templateId: string;

  @ApiPropertyOptional({ example: 'sp-123', description: 'ID of the sending profile' })
  @IsString()
  @IsOptional()
  sendingProfileId?: string | null;

  @ApiPropertyOptional({ example: 'lp-123', description: 'ID of the landing page used in the campaign' })
  @IsOptional()
  @IsString()
  landingPageId?: string;

  @ApiPropertyOptional({ example: 'lp-123', description: 'ID of the landing page used in the campaign' })
  @IsOptional()
  @IsString()
  recipientGroupId?: string | null;

  @ApiPropertyOptional({ example: '2025-07-15T10:00:00.000Z', description: 'Scheduled date for the campaign to launch' })
  @IsOptional()
  scheduledDate?: Date;

  @ApiPropertyOptional({ example: true, description: 'Whether the campaign should be sent immediately' })
  @IsOptional()
  @IsBoolean()
  sendImmediately?: boolean;

  @ApiPropertyOptional({ example: 'Special instructions for the campaign', description: 'Optional notes for the campaign' })
  @IsOptional()
  @IsString()
  notes?: string;
}

export class UpdatePesCampaignDto extends PartialType(CreatePesCampaignDto) {}

export class PesCampaignResponseDto {
  @ApiProperty()
  id: string;

  @ApiProperty()
  campaignId: string;

  @ApiProperty()
  name: string;

  @ApiProperty({ required: false })
  description: string;

  @ApiProperty({ enum: CampaignStatus })
  status: CampaignStatus;

  @ApiProperty({ type: String, format: 'date-time', required: false })
  scheduledDate: Date;

  @ApiProperty({ type: String, format: 'date-time', required: false })
  launchDate: Date;

  @ApiProperty({ type: String, format: 'date-time', required: false })
  completedDate: Date;

  @ApiProperty({ required: false })
  trackingId: string;

  @ApiProperty()
  sendImmediately: boolean;

  @ApiProperty({ required: false })
  notes: string;

  @ApiProperty({ type: String, format: 'date-time' })
  createdAt: Date;

  @ApiProperty({ type: String, format: 'date-time' })
  updatedAt: Date;

  @ApiPropertyOptional({ type: () => PesVerifiedUserResponseDto })
  verifiedUser?: PesVerifiedUserResponseDto | null;

  @ApiPropertyOptional({ type: () => PesTemplateResponseDto })
  template?: PesTemplateResponseDto | null;

  @ApiPropertyOptional({ type: () => PesSendingProfileResponseDto })
  sendingProfile?: PesSendingProfileResponseDto | null;

  @ApiProperty({ type: () => PesLandingPageResponseDto })
  landingPage?: PesLandingPageResponseDto | null;

  constructor(campaign: Partial<PesCampaign>) {
    this.id = campaign.id;
    this.campaignId = campaign.campaignId;
    this.name = campaign.name;
    this.description = campaign.description || '';
    this.status = campaign.status;
    this.scheduledDate = campaign.scheduledDate || null;
    this.launchDate = campaign.launchDate || null;
    this.completedDate = campaign.completedDate || null;
    this.trackingId = campaign.trackingId || '';
    this.sendImmediately = campaign.sendImmediately || false;
    this.notes = campaign.notes || '';
    this.createdAt = campaign.createdAt;
    this.updatedAt = campaign.updatedAt;
    this.verifiedUser = campaign.verifiedUser ? new PesVerifiedUserResponseDto(campaign.verifiedUser) : null;
    this.template = campaign.template ? new PesTemplateResponseDto(campaign.template) : null;
    this.sendingProfile = campaign.sendingProfile ? new PesSendingProfileResponseDto(campaign.sendingProfile) : null;
    this.landingPage = campaign.landingPage ? new PesLandingPageResponseDto(campaign.landingPage) : null;
  }
}
