import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsBoolean, IsEnum, IsOptional, IsString } from 'class-validator';
import { PaginationQueryDto } from '../../../common/dto/pagination-query.dto';
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 const USER_SORT_FIELDS = [
  'createdAt',
  'name',
  'username',
  'followersCount',
  'postsCount',
] as const;

export type UserSortField = (typeof USER_SORT_FIELDS)[number];

export class UserQueryDto extends PaginationQueryDto {
  @ApiPropertyOptional({ description: 'Search by name or username' })
  @IsOptional()
  @IsString()
  q?: string;

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

  @ApiPropertyOptional({ enum: MusicRole })
  @IsOptional()
  @IsEnum(MusicRole)
  musicRole?: MusicRole;

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

  @ApiPropertyOptional()
  @IsOptional()
  @Transform(toBoolean)
  @IsBoolean()
  isPrivate?: boolean;

  @ApiPropertyOptional({ description: 'Require or exclude users with avatar images' })
  @IsOptional()
  @Transform(toBoolean)
  @IsBoolean()
  hasAvatar?: boolean;

  @ApiPropertyOptional({ enum: USER_SORT_FIELDS, default: 'createdAt' })
  @IsOptional()
  @IsEnum(USER_SORT_FIELDS)
  sortBy?: UserSortField;
}
