import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsArray,
  ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';

/* ---------------- EMAIL CONTENT DTO ---------------- */

export class EmailTemplateContentDto {
  @ApiProperty({
    example: 'Apology for incorrect response',
    description: 'Subject line of the email',
  })
  @IsString()
  @IsNotEmpty({ message: 'Subject is required' })
  subject: string;

  @ApiProperty({
    example: '<html><body>Hello {{customer_name}}</body></html>',
    description: 'HTML body of the email template',
  })
  @IsString()
  @IsNotEmpty({ message: 'Email body (HTML) is required' })
  body: string;
}

/* ---------------- CREATE DTO ---------------- */

export class CreateEmailTemplateDto {
  @ApiProperty({
    example: 'admin-remark',
    description: 'Unique name/identifier of the email template',
  })
  @IsString()
  @IsNotEmpty({ message: 'Template name is required' })
  name: string;

  @ApiProperty({
    description: 'English email template content',
    type: EmailTemplateContentDto,
  })
  @ValidateNested()
  @Type(() => EmailTemplateContentDto)
  en: EmailTemplateContentDto;

  @ApiPropertyOptional({
    description: 'Hindi email template content',
    type: EmailTemplateContentDto,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => EmailTemplateContentDto)
  hi: EmailTemplateContentDto;

  @ApiPropertyOptional({
    description: 'French email template content',
    type: EmailTemplateContentDto,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => EmailTemplateContentDto)
  french: EmailTemplateContentDto;

  @ApiPropertyOptional({
    description: 'Spanish email template content',
    type: EmailTemplateContentDto,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => EmailTemplateContentDto)
  spanish: EmailTemplateContentDto;

  @ApiPropertyOptional({
    example: ['customer_name', 'admin_message'],
    description: 'Dynamic variables used inside the email template',
    type: [String],
  })
  @IsOptional()
  @IsArray({ message: 'Variables must be an array' })
  @IsString({ each: true, message: 'Each variable must be a string' })
  variables?: string[];
}

/* ---------------- UPDATE DTO ---------------- */

export class UpdateEmailTemplateDto {
  @ApiPropertyOptional({
    description: 'Updated English email template content',
    type: EmailTemplateContentDto,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => EmailTemplateContentDto)
  en?: EmailTemplateContentDto;

  @ApiPropertyOptional({
    description: 'Updated Hindi email template content',
    type: EmailTemplateContentDto,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => EmailTemplateContentDto)
  hi?: EmailTemplateContentDto;

  @ApiPropertyOptional({
    description: 'Updated French email template content',
    type: EmailTemplateContentDto,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => EmailTemplateContentDto)
  french?: EmailTemplateContentDto;

  @ApiPropertyOptional({
    description: 'Updated Spanish email template content',
    type: EmailTemplateContentDto,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => EmailTemplateContentDto)
  spanish?: EmailTemplateContentDto;

  @ApiPropertyOptional({
    example: ['customer_name', 'support_email'],
    description: 'Updated list of variables for the template',
    type: [String],
  })
  @IsOptional()
  @IsArray({ message: 'Variables must be an array' })
  @IsString({ each: true, message: 'Each variable must be a string' })
  variables?: string[];
}

/* ---------------- TEST EMAIL DTO ---------------- */

export class TestEmailDto {
  @ApiProperty({
    example: 'admin-remark',
    description: 'Name of the email template to test',
  })
  @IsString()
  @IsNotEmpty()
  name: string;
}
