import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform, Type } from 'class-transformer';
import { IsBoolean, IsEnum, IsNumber, IsOptional, IsString, Max, Min } from 'class-validator';
import { PaginationQueryDto } from '../../../common/dto/pagination-query.dto';
import { toBoolean } from '../../../common/utils/query-transform.util';
import { MarketplaceListingCondition } from '../enums/marketplace-listing-condition.enum';
import { MarketplaceListingCategory } from '../enums/marketplace-listing-category.enum';

export const INSTRUMENT_SORT_FIELDS = ['createdAt', 'updatedAt', 'price', 'title'] as const;
export type InstrumentSortField = (typeof INSTRUMENT_SORT_FIELDS)[number];

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

  @ApiPropertyOptional({ minimum: 0 })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(0)
  minPrice?: number;

  @ApiPropertyOptional({ minimum: 0 })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(0)
  maxPrice?: number;

  @ApiPropertyOptional({ default: true })
  @IsOptional()
  @Transform(toBoolean)
  @IsBoolean()
  isActive?: boolean;

  @ApiPropertyOptional({ enum: MarketplaceListingCondition })
  @IsOptional()
  @IsEnum(MarketplaceListingCondition)
  condition?: MarketplaceListingCondition;

  @ApiPropertyOptional({ description: 'Filter by instrument type such as oud, piano, violin' })
  @IsOptional()
  @IsString()
  instrumentType?: string;

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

  @ApiPropertyOptional({ enum: MarketplaceListingCategory })
  @IsOptional()
  @IsEnum(MarketplaceListingCategory)
  listingCategory?: MarketplaceListingCategory;

  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  @Max(200)
  limit?: number;
}
