import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import {
  IsArray,
  IsBoolean,
  IsEmail,
  IsEnum,
  IsNumber,
  IsOptional,
  IsString,
  IsUrl,
  Length,
  Matches,
  Max,
  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 UpdateUserDto {
  @ApiPropertyOptional({ example: 'John Doe' })
  @IsOptional()
  @IsString()
  @Length(2, 80)
  name?: string;

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

  @ApiPropertyOptional({ example: 'artist@example.com' })
  @IsOptional()
  @IsEmail()
  email?: string;

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

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

  @ApiPropertyOptional({ example: 'https://cdn.example.com/avatar.jpg' })
  @IsOptional()
  @IsUrl({ require_tld: false })
  avatar?: string;

  @ApiPropertyOptional({ example: 'https://cdn.example.com/profile-cover.jpg' })
  @IsOptional()
  @IsUrl({ require_tld: false })
  coverImage?: string;

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

  @ApiPropertyOptional({ required: false, example: 24.7136, minimum: -90, maximum: 90 })
  @IsOptional()
  @IsNumber()
  @Min(-90)
  @Max(90)
  latitude?: number;

  @ApiPropertyOptional({ required: false, example: 46.6753, minimum: -180, maximum: 180 })
  @IsOptional()
  @IsNumber()
  @Min(-180)
  @Max(180)
  longitude?: number;

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

  @ApiPropertyOptional({ default: false })
  @IsOptional()
  @Transform(toBoolean)
  @IsBoolean()
  isVerified?: boolean;

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

  @ApiPropertyOptional({ enum: ExperienceLevel })
  @IsOptional()
  @IsEnum(ExperienceLevel)
  experienceLevel?: ExperienceLevel;

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

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

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

  @ApiPropertyOptional({ example: 'طربي', maxLength: 80 })
  @IsOptional()
  @IsString()
  @Length(0, 80)
  preferredMood?: string;
}
