import { PartialType } from '@nestjs/mapped-types';
import { IsString, IsEnum, IsOptional, IsBoolean, IsArray, MaxLength, Length, IsNotEmpty } from 'class-validator';
import { TemplateCategory, DifficultyLevel } from '../enum/pes.enum';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { PesTemplate } from '../entities/pes-template.entity';

export class CreatePesTemplateDto {
  @ApiProperty({
    example: 'Phishing Awareness Template',
    description: 'Name of the template',
    maxLength: 255,
  })
  @IsString()
  @MaxLength(255)
  name: string;

  @ApiProperty({
    example: 'This template is used for phishing simulation emails.',
    description: 'Description of the template',
  })
  @IsString()
  description: string;

  @ApiProperty({
    enum: TemplateCategory,
    description: 'Category to which this template belongs',
  })
  @IsEnum(TemplateCategory)
  category: TemplateCategory;

  @ApiPropertyOptional({
    enum: DifficultyLevel,
    description: 'Optional difficulty level of the template',
  })
  @IsEnum(DifficultyLevel)
  @IsOptional()
  difficultyLevel?: DifficultyLevel;

  @ApiProperty({
    example: 'Security Alert: Your Account May Be at Risk',
    description: 'Subject line used in the email template',
    maxLength: 255,
  })
  @IsString()
  @MaxLength(255)
  subject: string;

  @ApiProperty({
    example: '<html><body><p>Click here to verify</p></body></html>',
    description: 'HTML content of the email',
  })
  @IsString()
  htmlContent: string;

  @ApiProperty({
    example: 'Click here to verify your account.',
    description: 'Plain text content of the email',
  })
  @IsString()
  textContent: string;

  @ApiPropertyOptional({
    example: ['Urgent', 'Verify Now'],
    description: 'List of red flag keywords or phrases used in the template',
    type: [String],
  })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  redFlags?: string[];

  @ApiPropertyOptional({
    example: true,
    description: 'Indicates whether this is a system-defined template',
  })
  @IsOptional()
  @IsBoolean()
  isSystemTemplate?: boolean;

  @ApiPropertyOptional({
    example: true,
    description: 'Indicates whether the template is active',
  })
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;

  @ApiPropertyOptional({
    example: '123e4567-e89b-12d3-a456-426614174000',
    maxLength: 36,
    format: 'uuid',
    description: 'Identifier UUID of the user who created the template',
  })
  @IsString()
  @MaxLength(36)
  @Length(36, 36)
  @IsNotEmpty()
  createdBy: string;

  @ApiPropertyOptional({
    example: 'Use with caution for finance departments.',
    description: 'Optional notes about the template',
  })
  @IsOptional()
  @IsString()
  notes?: string;
}

export class UpdatePesTemplateDto extends PartialType(CreatePesTemplateDto) {}

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

  @ApiProperty()
  templateId: string;

  @ApiProperty()
  name: string;

  @ApiProperty()
  description: string;

  @ApiProperty({ enum: TemplateCategory })
  category: TemplateCategory;

  @ApiProperty({ enum: DifficultyLevel })
  difficultyLevel: DifficultyLevel;

  @ApiProperty()
  subject: string;

  @ApiProperty()
  htmlContent: string;

  @ApiProperty()
  textContent: string;

  @ApiProperty({ type: [String], required: false })
  redFlags: string[];

  @ApiProperty()
  isSystemTemplate: boolean;

  @ApiProperty()
  isActive: boolean;

  @ApiProperty()
  usageCount: number;

  @ApiProperty({ type: 'number', format: 'decimal' })
  successRate: number;

  @ApiProperty({ nullable: true })
  createdBy?: string;

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

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

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

  constructor(pesTemplate: PesTemplate) {
    this.id = pesTemplate.id;
    this.templateId = pesTemplate.templateId;
    this.name = pesTemplate.name;
    this.description = pesTemplate.description || '';
    this.category = pesTemplate.category;
    this.difficultyLevel = pesTemplate.difficultyLevel || DifficultyLevel.BEGINNER;
    this.subject = pesTemplate.subject || '';
    this.htmlContent = pesTemplate.htmlContent || '';
    this.textContent = pesTemplate.textContent || '';
    this.redFlags = pesTemplate.redFlags || [];
    this.isSystemTemplate = pesTemplate.isSystemTemplate || false;
    this.isActive = pesTemplate.isActive;
    this.usageCount = pesTemplate.usageCount || 0;
    this.successRate = pesTemplate.successRate || 0.0;
    // this.createdBy = pesTemplate.createdBy || null;
    this.notes = pesTemplate.notes || null;
    this.createdAt = pesTemplate.createdAt;
    this.updatedAt = pesTemplate.updatedAt;
  }
}
