import { Transform } from 'class-transformer';
import {
  IsBoolean,
  IsEmail,
  IsOptional,
  IsString,
  MaxLength,
  ValidateIf,
} from 'class-validator';

function normalizeEmail(value: unknown): string | null {
  if (typeof value !== 'string') {
    return null;
  }

  const normalizedEmail = value.trim().toLowerCase();

  return normalizedEmail.length > 0 ? normalizedEmail : null;
}

export class UpdateNotificationSettingDto {
  @IsBoolean()
  systemEnabled!: boolean;

  @IsBoolean()
  unreadBadge!: boolean;

  @IsBoolean()
  gmailEnabled!: boolean;

  @Transform(({ value }: { value: unknown }) => normalizeEmail(value))
  @ValidateIf((dto: UpdateNotificationSettingDto) => dto.gmailEnabled === true)
  @IsString()
  @IsEmail()
  @MaxLength(255)
  gmailAddress?: string | null;

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