import { ApiPropertyOptional } from '@nestjs/swagger';
import { ListingStatus, PropertyType, TransactionType } from '@prisma/client';
import { Transform, Type } from 'class-transformer';
import { IsEnum, IsIn, IsISO8601, IsNumber, IsOptional, IsString, Max, Min } from 'class-validator';
import { type ListingSortBy } from '../../domain/repositories/listing.repository';

const LISTING_SORT_BY_VALUES: ListingSortBy[] = ['publishedAt', 'priceAsc', 'priceDesc', 'createdAt'];
const LISTING_SORT_ORDER_VALUES = ['asc', 'desc'] as const;
export type ListingSortOrder = (typeof LISTING_SORT_ORDER_VALUES)[number];

export class SearchListingsDto {
  @ApiPropertyOptional({ enum: ListingStatus, example: 'ACTIVE', description: 'Filter by listing status' })
  @IsOptional()
  @IsEnum(ListingStatus)
  status?: ListingStatus;

  @ApiPropertyOptional({ enum: TransactionType, example: 'SALE', description: 'Filter by transaction type' })
  @IsOptional()
  @IsEnum(TransactionType)
  transactionType?: TransactionType;

  @ApiPropertyOptional({ enum: PropertyType, example: 'APARTMENT', description: 'Filter by property type' })
  @IsOptional()
  @IsEnum(PropertyType)
  propertyType?: PropertyType;

  @ApiPropertyOptional({ example: 'Hồ Chí Minh', description: 'Filter by city / province' })
  @IsOptional()
  @IsString()
  city?: string;

  @ApiPropertyOptional({ example: 'Quận 2', description: 'Filter by district' })
  @IsOptional()
  @IsString()
  district?: string;

  @ApiPropertyOptional({ type: String, example: '2000000000', description: 'Minimum price in VND' })
  @IsOptional()
  @Transform(({ value }) => (value != null ? BigInt(value) : undefined))
  minPrice?: bigint;

  @ApiPropertyOptional({ type: String, example: '10000000000', description: 'Maximum price in VND' })
  @IsOptional()
  @Transform(({ value }) => (value != null ? BigInt(value) : undefined))
  maxPrice?: bigint;

  @ApiPropertyOptional({ example: 50, description: 'Minimum area in square metres' })
  @IsOptional()
  @IsNumber()
  @Type(() => Number)
  minArea?: number;

  @ApiPropertyOptional({ example: 200, description: 'Maximum area in square metres' })
  @IsOptional()
  @IsNumber()
  @Type(() => Number)
  maxArea?: number;

  @ApiPropertyOptional({ example: 2, description: 'Filter by number of bedrooms' })
  @IsOptional()
  @IsNumber()
  @Type(() => Number)
  bedrooms?: number;

  @ApiPropertyOptional({ example: 1, description: 'Page number (starts at 1)' })
  @IsOptional()
  @IsNumber()
  @Type(() => Number)
  @Min(1)
  page?: number;

  @ApiPropertyOptional({ example: 20, description: 'Items per page (max 100)' })
  @IsOptional()
  @IsNumber()
  @Type(() => Number)
  @Min(1)
  @Max(100)
  limit?: number;

  @ApiPropertyOptional({
    enum: LISTING_SORT_BY_VALUES,
    example: 'publishedAt',
    description: 'Sort field. Defaults to publishedAt with featured listings first.',
  })
  @IsOptional()
  @IsIn(LISTING_SORT_BY_VALUES)
  sortBy?: ListingSortBy;

  @ApiPropertyOptional({
    enum: LISTING_SORT_ORDER_VALUES,
    example: 'desc',
    description: 'Sort direction (asc | desc). Defaults to desc.',
  })
  @IsOptional()
  @IsIn(LISTING_SORT_ORDER_VALUES)
  order?: ListingSortOrder;

  @ApiPropertyOptional({
    type: String,
    example: '2026-04-21T00:00:00.000Z',
    description: 'Return only listings with publishedAt > newSince (ISO-8601 timestamp). Used for delta pulls by the FE ticker.',
  })
  @IsOptional()
  @IsISO8601()
  newSince?: string;
}
