import { getLocaleText } from '@hed-hog/api-locale';
import { Transform } from 'class-transformer';
import {
  IsBoolean,
  IsEmail,
  IsIn,
  IsInt,
  IsOptional,
  IsString,
  MaxLength,
  Min,
} from 'class-validator';

const providerOptions = ['SMTP', 'GMAIL', 'SES'] as const;

export class CreateDTO {
  @IsString({ message: (args) => getLocaleText('validation.stringRequired', args.value) })
  @MaxLength(127)
  @Transform(({ value }) => String(value ?? '').trim())
  slug: string;

  @IsString({ message: (args) => getLocaleText('validation.stringRequired', args.value) })
  @MaxLength(127)
  @Transform(({ value }) => String(value ?? '').trim())
  name: string;

  @IsIn(providerOptions)
  @Transform(({ value }) => String(value ?? '').trim().toUpperCase())
  provider: (typeof providerOptions)[number];

  @IsOptional()
  @IsString()
  @MaxLength(127)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value).trim(),
  )
  from_name?: string;

  @IsEmail({}, { message: 'Invalid email address.' })
  @MaxLength(255)
  @Transform(({ value }) => String(value ?? '').trim())
  from_email: string;

  @IsOptional()
  @IsString()
  @MaxLength(127)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value).trim(),
  )
  reply_to_name?: string;

  @IsOptional()
  @IsEmail({}, { message: 'Invalid email address.' })
  @MaxLength(255)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value).trim(),
  )
  reply_to_email?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value).trim(),
  )
  smtp_host?: string;

  @IsOptional()
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : Number(value),
  )
  @IsInt()
  @Min(1)
  smtp_port?: number;

  @IsOptional()
  @Transform(({ value }) =>
    value === true || value === 'true' || value === 1 || value === '1',
  )
  @IsBoolean()
  smtp_secure?: boolean;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value).trim(),
  )
  smtp_username?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value),
  )
  smtp_password?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value).trim(),
  )
  gmail_client_id?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value),
  )
  gmail_client_secret?: string;

  @IsOptional()
  @IsString()
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value),
  )
  gmail_refresh_token?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value).trim(),
  )
  ses_access_key_id?: string;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value),
  )
  ses_secret_access_key?: string;

  @IsOptional()
  @IsString()
  @MaxLength(127)
  @Transform(({ value }) =>
    value === undefined || value === null || value === '' ? undefined : String(value).trim(),
  )
  ses_region?: string;
}
