/**
 * Email Job DTO
 * 
 * Data Transfer Object for queuing email jobs
 */

import { IsString, IsOptional, IsObject, IsArray, ValidateNested, IsEmail } from 'class-validator';
import { Type } from 'class-transformer';

class EmailRecipient {
  @IsEmail()
  emailAddress: string;

  @IsString()
  @IsOptional()
  name?: string;
}

class EmailSender {
  @IsString()
  address: string;

  @IsString()
  name: string;
}

class EmailAttachment {
  @IsString()
  name: string;

  /**
   * Base64 encoded content (for small attachments only).
   * For large files like PDFs, prefer using S3 metadata fields below
   * to avoid hitting SQS 256 KB message size limits.
   */
  @IsString()
  @IsOptional()
  content?: string;

  /**
   * S3 object key for the attachment.
   * When provided, the actual file will be downloaded from S3
   * just before sending the email.
   */
  @IsString()
  @IsOptional()
  s3Key?: string;

  /**
   * Optional S3 bucket override. If not provided, the default
   * application bucket will be used.
   */
  @IsString()
  @IsOptional()
  s3Bucket?: string;

  @IsString()
  contentType: string;
}

export class QueueEmailDto {
  @ValidateNested()
  @Type(() => EmailRecipient)
  to: EmailRecipient;

  @ValidateNested()
  @Type(() => EmailSender)
  from: EmailSender;

  @IsString()
  subject: string;

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

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

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

  @IsObject()
  @IsOptional()
  templateData?: Record<string, any>;

  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => EmailRecipient)
  @IsOptional()
  cc?: EmailRecipient[];

  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => EmailRecipient)
  @IsOptional()
  bcc?: EmailRecipient[];

  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => EmailAttachment)
  @IsOptional()
  attachments?: EmailAttachment[];

  @ValidateNested()
  @Type(() => EmailRecipient)
  @IsOptional()
  replyTo?: EmailRecipient;

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

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

  @IsObject()
  @IsOptional()
  metadata?: Record<string, any>;
}
