import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsEmail, IsEnum, IsString, Matches, MaxLength, MinLength, ValidateIf, Validate, IsOptional, IsBoolean, Length } from 'class-validator';
import { UserRole, UserStatus } from 'common/enum/UserRole.enum';
import { ReferralMailValidator } from 'common/validators/referal-mail.validator';

export class UpdateUserDto {
  @ApiPropertyOptional({ example: 'John', description: 'First name of the user' })
  @IsOptional()
  @IsString()
  @Length(1, 60)
  firstName?: string;

  @ApiPropertyOptional({ example: 'Doe', description: 'Last name of the user' })
  @IsOptional()
  @IsString()
  @Length(1, 60)
  lastName?: string;

  @ApiPropertyOptional({ example: 'john.doe@example.com', description: 'Email of the user' })
  @IsOptional()
  @IsEmail({}, { message: 'Invalid email format' })
  @IsString({ message: 'Email must be a string' })
  email?: string;

  @ApiPropertyOptional({
    example: 'Password123!',
    description:
      'Password must be 6-32 characters long, include at least one uppercase letter, one lowercase letter, one number, and one special character',
  })
  @IsOptional()
  @IsString({ message: 'Password must be a string' })
  @MinLength(6, { message: 'Password must be at least 6 characters long' })
  @MaxLength(32, { message: 'Password cannot be longer than 32 characters' })
  @Matches(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]/, {
    message: 'Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character (@$!%*?&)',
  })
  password?: string;

  @ApiPropertyOptional({
    enum: UserRole,
    example: UserRole.USER,
    description: `User role`,
  })
  @IsOptional()
  @IsEnum(UserRole, { message: 'Invalid role' })
  role?: UserRole;

  @ApiPropertyOptional({ example: 'client-id', description: 'Client ID' })
  @IsOptional()
  clientId?: string;

  @ApiPropertyOptional({ enum: UserStatus, description: 'Status of the user' })
  @IsOptional()
  @IsEnum(UserStatus)
  status?: UserStatus;

  @ValidateIf(dto => dto.role === UserRole.ADMIN || dto.role === UserRole.SUPER_ADMIN)
  @ApiPropertyOptional({
    example: 'referral@example.com',
    description: 'Referral email (required if role is ADMIN or SUPERADMIN)',
  })
  @IsOptional()
  @IsEmail({}, { message: 'Invalid referral email format' })
  @Validate(ReferralMailValidator, { message: 'Referral mail must exist and have a superior role' })
  referralMail?: string;

  @ApiPropertyOptional({
    example: 'https://cdn-icons-png.flaticon.com/512/149/149071.png',
    description: 'Profile picture URL',
  })
  @IsOptional()
  @IsString()
  picture?: string;

  @ApiPropertyOptional({
    example: 'googleId123',
    description: 'Google ID',
  })
  @IsOptional()
  @IsString({ message: 'Google ID must be a string' })
  googleId?: string;

  @ApiPropertyOptional({
    example: false,
    description: 'Is Google user',
  })
  @IsOptional()
  @IsBoolean({ message: 'Is Google user must be a boolean' })
  isGoogleUser?: boolean;
}
