import { WagonTransferRequestStatus } from '@edr/types';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsIn, IsOptional, IsUUID } from 'class-validator';

import { PaginationQueryDto } from '../../../common/dto/pagination-query.dto';

const SORT_FIELDS = ['createdAt', 'quantity', 'status'] as const;

/**
 * Transfer-desk list query. `status` accepts a comma-separated list so the
 * "Open" tab can ask for PENDING + PARTIALLY_FULFILLED in one call.
 */
export class ListTransferRequestsQueryDto extends PaginationQueryDto {
  @ApiPropertyOptional({
    description: 'One status or a comma-separated list',
    enum: WagonTransferRequestStatus,
  })
  @IsOptional()
  @Transform(({ value }) =>
    typeof value === 'string' && value.trim() ? value.trim() : undefined,
  )
  status?: string;

  @ApiPropertyOptional({ description: 'Source yard' })
  @IsOptional()
  @IsUUID()
  fromYardId?: string;

  @ApiPropertyOptional({ description: 'Destination yard' })
  @IsOptional()
  @IsUUID()
  toYardId?: string;

  @ApiPropertyOptional({ description: 'Wagon type' })
  @IsOptional()
  @IsUUID()
  wagonTypeId?: string;

  @ApiPropertyOptional({ enum: SORT_FIELDS, default: 'createdAt' })
  @IsOptional()
  @IsIn([...SORT_FIELDS])
  sortBy?: (typeof SORT_FIELDS)[number];
}
