import { IsEmail, IsInt, IsOptional, IsBoolean, IsString } from 'class-validator';
import { Type } from 'class-transformer';
import { ApiProperty, PartialType } from '@nestjs/swagger';
import { PesCampaign } from '../entities/pes-campaign.entity';
import { PesTemplate } from '../entities/pes-template.entity';
import { PesSendingProfile } from '../entities/pes-sending-profiles.entity';
import { PesVerifiedUsers } from '../entities/pes_verified_users.entity';
import { PesCampaignResponseDto } from './pes-campaign.dto';
import { PesTemplateResponseDto } from './pes-template.dto';
import { PesSendingProfileResponseDto } from './pes-sending-profile.dto';

export class CreatePesVerifiedUserDto {
  @IsEmail()
  email: string;

  @IsInt()
  userId: number;

  @IsOptional()
  @IsInt()
  dailyEmailCount?: number;

  @IsOptional()
  @IsInt()
  totalEmailCount?: number;

  @IsOptional()
  @IsInt()
  totalCampaigns?: number;

  @IsOptional()
  @Type(() => Date)
  lastEmailDate?: Date;

  @IsOptional()
  @IsBoolean()
  isBlocked?: boolean;

  @IsOptional()
  @IsString()
  blockReason?: string;

  @IsOptional()
  @Type(() => Date)
  blockedAt?: Date;
}

export class UpdatePesVerifiedUserDto extends PartialType(CreatePesVerifiedUserDto) {}

export class PesVerifiedUserResponseDto {
  @ApiProperty({ example: 'uuid-v4' })
  id: string;

  @ApiProperty()
  email: string;

  @ApiProperty()
  userId: number;

  @ApiProperty()
  dailyEmailCount: number;

  @ApiProperty()
  totalEmailCount: number;

  @ApiProperty()
  totalCampaigns: number;

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

  @ApiProperty({ example: true })
  isBlocked: boolean;

  @ApiProperty({ nullable: true })
  blockReason: string;

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

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

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

  @ApiProperty({ type: () => [PesCampaign] })
  campaigns: PesCampaignResponseDto[];

  @ApiProperty({ type: () => [PesTemplate] })
  templates: PesTemplateResponseDto[];

  @ApiProperty({ type: () => [PesSendingProfile] })
  sendingProfiles: PesSendingProfileResponseDto[];

  constructor(pesVerifiedUser: PesVerifiedUsers) {
    this.id = pesVerifiedUser.id;
    this.email = pesVerifiedUser.email;
    this.userId = pesVerifiedUser.userId;
    this.dailyEmailCount = pesVerifiedUser.dailyEmailCount || 0;
    this.totalEmailCount = pesVerifiedUser.totalEmailCount || 0;
    this.totalCampaigns = pesVerifiedUser.totalCampaigns || 0;
    this.lastEmailDate = pesVerifiedUser.lastEmailDate || null;
    this.isBlocked = pesVerifiedUser.isBlocked || false;
    this.blockReason = pesVerifiedUser.blockReason || null;
    this.blockedAt = pesVerifiedUser.blockedAt || null;
    this.createdAt = pesVerifiedUser.createdAt;
    this.updatedAt = pesVerifiedUser.updatedAt;
    this.campaigns = (pesVerifiedUser.campaigns || []).map(campaign => new PesCampaignResponseDto(campaign));
    this.templates = (pesVerifiedUser.templates || []).map(template => new PesTemplateResponseDto(template));
    this.sendingProfiles = (pesVerifiedUser.sendingProfiles || []).map(profile => new PesSendingProfileResponseDto(profile));
  }
}
