import { ApiProperty } from '@nestjs/swagger';
import { Transform, Type } from 'class-transformer';
import {
  IsArray,
  IsBoolean,
  IsEmail,
  IsEnum,
  IsNumber,
  IsOptional,
  IsString,
  Length,
  Max,
  Matches,
  Min,
} from 'class-validator';
import { ExperienceLevel } from '../../../common/enums/experience-level.enum';
import { MusicRole } from '../../../common/enums/music-role.enum';
import { toBoolean } from '../../../common/utils/query-transform.util';

export class RegisterDto {
  @ApiProperty({ example: 'john@example.com' })
  @IsEmail()
  email!: string;

  @ApiProperty({ minLength: 8 })
  @IsString()
  @Length(8, 64)
  password!: string;

  @ApiProperty({ minLength: 8 })
  @IsString()
  @Length(8, 64)
  confirmPassword!: string;

  @ApiProperty({ required: false, example: 'John Doe' })
  @IsOptional()
  @IsString()
  @Length(2, 80)
  name?: string;

  @ApiProperty({ required: false, example: 'john_doe' })
  @IsOptional()
  @IsString()
  @Length(3, 30)
  @Matches(/^[a-zA-Z0-9_.]+$/, { message: 'username can contain letters, numbers, _ and .' })
  username?: string;

  @ApiProperty({ required: false, example: 'Artist One' })
  @IsOptional()
  @IsString()
  @Length(0, 80)
  stageName?: string;

  @ApiProperty({ required: false, maxLength: 160 })
  @IsOptional()
  @IsString()
  @Length(0, 160)
  bio?: string;

  @ApiProperty({ required: false, example: 'Riyadh, Saudi Arabia' })
  @IsOptional()
  @IsString()
  @Length(0, 120)
  location?: string;

  @ApiProperty({ example: 24.7136, minimum: -90, maximum: 90 })
  @Type(() => Number)
  @IsNumber()
  @Min(-90)
  @Max(90)
  latitude!: number;

  @ApiProperty({ example: 46.6753, minimum: -180, maximum: 180 })
  @Type(() => Number)
  @IsNumber()
  @Min(-180)
  @Max(180)
  longitude!: number;

  @ApiProperty({ required: false, default: false })
  @IsOptional()
  @Transform(toBoolean)
  @IsBoolean()
  isPrivate?: boolean;

  @ApiProperty({ required: false, enum: MusicRole, isArray: true })
  @IsOptional()
  @IsArray()
  @IsEnum(MusicRole, { each: true })
  musicRoles?: MusicRole[];

  @ApiProperty({ required: false, enum: ExperienceLevel, example: ExperienceLevel.BEGINNER })
  @IsOptional()
  @IsEnum(ExperienceLevel)
  experienceLevel?: ExperienceLevel;

  @ApiProperty({ required: false, type: [String], example: ['Tarab', 'Pop'] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  musicGenres?: string[];

  @ApiProperty({ required: false, type: [String], example: ['Oud', 'Piano'] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  favoriteInstruments?: string[];

  @ApiProperty({ required: false, type: [String], example: ['Bayati', 'Rast'] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  favoriteMaqamat?: string[];
}
