import { Transform } from 'class-transformer';
import { PartialType } from '@nestjs/mapped-types';
import { IsIn, IsInt, IsOptional, IsString } from 'class-validator';

const toNullableInteger = ({ value }: { value: unknown }) => {
  if (value === undefined || value === null || value === '') {
    return undefined;
  }

  return Number(value);
};

const toOptionalTrimmedString = ({ value }: { value: unknown }) => {
  if (value === undefined || value === null) {
    return undefined;
  }

  return String(value).trim();
};

export class CreateWebhookActionDTO {
  @IsIn(['email', 'whatsapp_evolution'])
  type: 'email' | 'whatsapp_evolution';

  @IsOptional()
  @Transform(toOptionalTrimmedString)
  @IsString()
  name?: string;

  @IsOptional()
  @Transform(toNullableInteger)
  @IsInt()
  order?: number;

  @IsOptional()
  @IsIn(['active', 'inactive'])
  status?: 'active' | 'inactive';

  @IsOptional()
  @Transform(toNullableInteger)
  @IsInt()
  mail_profile_id?: number;

  @IsOptional()
  @Transform(toNullableInteger)
  @IsInt()
  mail_id?: number;

  @IsOptional()
  @Transform(toOptionalTrimmedString)
  @IsString()
  email_to?: string;

  @IsOptional()
  @Transform(toOptionalTrimmedString)
  @IsString()
  email_cc?: string;

  @IsOptional()
  @Transform(toOptionalTrimmedString)
  @IsString()
  email_bcc?: string;

  @IsOptional()
  @IsIn(['phone', 'group'])
  whatsapp_target_type?: 'phone' | 'group';

  @IsOptional()
  @Transform(toOptionalTrimmedString)
  @IsString()
  whatsapp_target?: string;

  @IsOptional()
  @Transform(toOptionalTrimmedString)
  @IsString()
  whatsapp_template?: string;

  @IsOptional()
  @Transform(toOptionalTrimmedString)
  @IsString()
  whatsapp_instance?: string;

  @IsOptional()
  @Transform(toOptionalTrimmedString)
  @IsString()
  whatsapp_base_url?: string;

  @IsOptional()
  @Transform(toOptionalTrimmedString)
  @IsString()
  whatsapp_token?: string;
}

export class UpdateWebhookActionDTO extends PartialType(
  CreateWebhookActionDTO,
) {}