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

export class CreateUserDto {
  @ApiProperty({ example: 'John', description: 'First name of the user' })
  @IsString()
  @IsNotEmpty()
  @Length(1, 60)
  firstName: string;

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

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

  @ApiProperty({
    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',
  })
  @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;

  @ApiProperty({
    enum: UserRole,
    example: UserRole.USER,
    description: `User role (default: ${UserRole.USER})`,
    default: UserRole.USER,
    required: false,
  })
  @IsEnum(UserRole, { message: 'Invalid role' })
  role: UserRole = UserRole.USER;

  @ApiPropertyOptional({
    example: false,
    description: 'Is email verified (default: false)',
  })
  @IsOptional()
  @IsBoolean({ message: 'Is email verified must be a boolean' })
  isEmailVerified?: boolean = false; // Default value is false

  @ApiProperty({ enum: UserStatus, default: UserStatus.ACTIVE, description: 'Status of the user' })
  @IsEnum(UserStatus)
  @IsOptional()
  status?: UserStatus = UserStatus.ACTIVE; // Default status is ACTIVE

  @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)',
  })
  @IsEmail({}, { message: 'Invalid referral email format' })
  @Validate(ReferralMailValidator, { message: 'Referral mail must exist and have a superior role' })
  referralMail?: string;

  // iscustom
  @ApiPropertyOptional({
    example: true,
    description: 'Is custom user (default: false)',
  })
  @IsOptional()
  @IsBoolean({ message: 'Is custom user must be a boolean' })
  isCustom?: boolean = false;

  @ApiProperty({
    example: 'https://cdn-icons-png.flaticon.com/512/149/149071.png',
    description: 'Profile picture URL',
    required: false,
  })
  @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;
}
