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

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({
    format: 'uuid',
    description:
      'Narrow to a single operational profile (importer/exporter/freight_forwarder) within the company.',
  })
  @IsOptional()
  @IsUUID()
  companyProfileId?: 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: BOOKING_TYPES, description: 'ONE_TIME or GENERAL_CONTRACT' })
  @IsOptional()
  @IsIn([...BOOKING_TYPES])
  bookingType?: string;

  @ApiPropertyOptional({ description: 'Filter bookings created on/after this date (ISO)' })
  @IsOptional()
  @IsDateString()
  createdFrom?: string;

  @ApiPropertyOptional({ description: 'Filter bookings created on/before this date (ISO)' })
  @IsOptional()
  @IsDateString()
  createdTo?: string;

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

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

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

  @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({
    description: 'Comma-separated scheduling statuses (NOT_SCHEDULED,HOLDING,ELIGIBLE,SCHEDULED)',
  })
  @IsOptional()
  @Transform(({ value }) => {
    if (value === undefined || value === null || value === '') return undefined;
    if (Array.isArray(value)) return value.map(String).join(',');
    return String(value);
  })
  schedulingStatuses?: string;

  @ApiPropertyOptional({ enum: ['true', 'false'], description: 'Filter by train schedule assignment' })
  @IsOptional()
  @IsIn(['true', 'false'])
  assignedToSchedule?: 'true' | 'false';

  @ApiPropertyOptional({ default: 'createdAt' })
  @IsOptional()
  @IsIn(['createdAt', 'priorityScore', 'scheduledDate', 'isGovernment'])
  sortBy?: string;

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