import { IsBoolean, IsEnum, IsOptional, IsString, IsEmail, MaxLength, IsNumber, IsNotEmpty, MinLength } from 'class-validator';
import { SendingProfileType } from '../enum/pes.enum';
import { PartialType } from '@nestjs/mapped-types';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { PesSendingProfile } from '../entities/pes-sending-profiles.entity';

export class CreatePesSendingProfileDto {
  @ApiProperty({
    example: 'Marketing Profile',
    description: 'Name of the sending profile',
    maxLength: 255,
  })
  @IsString()
  @MaxLength(255)
  @MinLength(3)
  @IsNotEmpty()
  name: string;

  @ApiPropertyOptional({
    example: 'Profile used for marketing emails',
    description: 'Optional description of the sending profile',
  })
  @IsOptional()
  @IsString()
  description?: string;

  @ApiProperty({
    example: 'no-reply@example.com',
    description: 'The email address used in the "From" field of the email',
    maxLength: 255,
  })
  @IsEmail()
  @MaxLength(255)
  @IsNotEmpty()
  fromAddress: string;

  @ApiPropertyOptional({
    example: 'Example Company',
    description: 'Display name to show in the "From" field',
    maxLength: 255,
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(255)
  fromName: string;

  @ApiPropertyOptional({
    enum: SendingProfileType,
    description: 'Type of the sending profile (e.g., SMTP, internal)',
  })
  @IsOptional()
  @IsEnum(SendingProfileType)
  profileType: SendingProfileType;

  @ApiPropertyOptional({
    example: 'smtp.example.com',
    description: 'SMTP server host address',
    maxLength: 255,
  })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  smtpHost: string | null;

  @ApiPropertyOptional({
    example: 587,
    description: 'SMTP server port',
  })
  @IsNumber()
  @IsOptional()
  smtpPort: number | null;

  @ApiPropertyOptional({
    example: 'smtp_user',
    description: 'SMTP username used for authentication',
    maxLength: 255,
  })
  @IsString()
  @MaxLength(255)
  @IsOptional()
  smtpUsername: string | null;

  @ApiPropertyOptional({
    example: 'securepassword123',
    description: 'SMTP password used for authentication',
  })
  @IsOptional()
  @IsString()
  smtpPassword: string | null;

  @ApiPropertyOptional({
    example: true,
    description: 'Indicates whether to use TLS for SMTP',
  })
  @IsBoolean()
  @IsOptional()
  smtpTls: boolean | null;

  @ApiPropertyOptional({
    example: 42,
    description: 'User ID who owns the sending profile',
  })
  @IsString()
  @IsNotEmpty()
  verifiedUserId: string;

  @ApiPropertyOptional({
    example: true,
    description: 'Whether the sending profile is active',
  })
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;
}

export class UpdatePesSendingProfileDto extends PartialType(CreatePesSendingProfileDto) {}

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

  @ApiProperty()
  profileId: string;

  @ApiProperty()
  name: string;

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

  @ApiProperty()
  fromAddress: string;

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

  @ApiProperty({ enum: SendingProfileType })
  profileType: SendingProfileType;

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

  @ApiProperty({ required: false })
  smtpPort: number;

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

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

  @ApiProperty()
  smtpTls: boolean;

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

  @ApiProperty()
  isActive: boolean;

  @ApiProperty()
  isDefault: boolean;

  @ApiProperty()
  usageCount: number;

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

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

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

  constructor(pesSendingProfile: PesSendingProfile) {
    this.id = pesSendingProfile.id;
    this.profileId = pesSendingProfile.profileId;
    this.name = pesSendingProfile.name;
    this.description = pesSendingProfile.description || null;
    this.fromAddress = pesSendingProfile.fromAddress;
    this.fromName = pesSendingProfile.fromName || null;
    this.profileType = pesSendingProfile.profileType;
    this.smtpHost = pesSendingProfile.smtpHost || null;
    this.smtpPort = pesSendingProfile.smtpPort || null;
    this.smtpUsername = pesSendingProfile.smtpUsername || null;
    this.smtpPassword = pesSendingProfile.smtpPassword || null;
    this.smtpTls = pesSendingProfile.smtpTls || false;
    this.verifiedUserId = pesSendingProfile.verifiedUserId || null;
    this.isActive = pesSendingProfile.isActive || true;
    this.isDefault = pesSendingProfile.isDefault || false;
    this.usageCount = pesSendingProfile.usageCount || 0;
    this.lastUsedAt = pesSendingProfile.lastUsedAt || null;
    this.createdAt = pesSendingProfile.createdAt;
    this.updatedAt = pesSendingProfile.updatedAt;
  }
}
