import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsIn, IsOptional, IsUUID } from 'class-validator';
import {
  BOOKING_STATUSES,
  FREIGHT_TYPES,
  PAYMENT_CURRENCIES,
  TRADE_DIRECTIONS,
} from './create-booking.dto';

export class FilterBookingDto {
  @ApiPropertyOptional({ enum: BOOKING_STATUSES })
  @IsOptional()
  @IsIn([...BOOKING_STATUSES])
  status?: string;

  @ApiPropertyOptional({
    description:
      'Filter by statuses: comma-separated (PENDING_APPROVAL,APPROVED) or repeated query params. Overrides status when set.',
  })
  @IsOptional()
  @Transform(({ value }) => {
    if (value === undefined || value === null || value === '') return undefined;
    if (Array.isArray(value)) return value.map(String).join(',');
    return String(value);
  })
  statuses?: string;

  // @ApiPropertyOptional({ format: 'uuid' })
  // @IsOptional()
  // @IsUUID()
  // customerId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  companyId?: string;

  @ApiPropertyOptional()
  @IsOptional()
  contractType?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  serviceTypeId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  cargoTypeId?: string;

  @ApiPropertyOptional({ enum: FREIGHT_TYPES })
  @IsOptional()
  @IsIn([...FREIGHT_TYPES])
  freightType?: string;

  @ApiPropertyOptional({ enum: TRADE_DIRECTIONS })
  @IsOptional()
  @IsIn([...TRADE_DIRECTIONS])
  tradeDirection?: string;

  @ApiPropertyOptional({ enum: PAYMENT_CURRENCIES })
  @IsOptional()
  @IsIn([...PAYMENT_CURRENCIES])
  paymentCurrency?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @Transform(({ value }) => value === 'true' || value === true)
  allowConsolidation?: boolean;

  @ApiPropertyOptional({ description: 'true | false — filter paired consolidation' })
  @IsOptional()
  consolidationPaired?: string;

  @ApiPropertyOptional({ default: 1 })
  @IsOptional()
  @Transform(({ value }) => (value ? parseInt(value, 10) : 1))
  page?: number;

  @ApiPropertyOptional({ default: 20 })
  @IsOptional()
  @Transform(({ value }) => (value ? parseInt(value, 10) : 20))
  pageSize?: number;

  @ApiPropertyOptional({ default: 'createdAt' })
  @IsOptional()
  sortBy?: string;

  @ApiPropertyOptional({ enum: ['ASC', 'DESC'], default: 'DESC' })
  @IsOptional()
  @IsIn(['ASC', 'DESC'])
  sortOrder?: 'ASC' | 'DESC';
}
