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

import {
  LOCOMOTIVE_STATUSES,
  LOCOMOTIVE_TYPES,
} from '../entities/locomotive.entity';

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

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

  @ApiPropertyOptional({ description: 'Filter by current yard' })
  @IsOptional()
  @IsUUID()
  currentYardId?: string;

  /**
   * Drop locomotives already coupled to a built train — the train-builder
   * "change locomotives" picker uses this so a loco that belongs to another
   * train is never offered (the backend would 409 on save anyway). Combine with
   * `excludeTrainId` to keep the CURRENT train's own locos in the list.
   */
  @ApiPropertyOptional({
    description: 'Exclude locomotives already coupled to any built train',
  })
  @IsOptional()
  @Transform(({ value }) => value === true || value === 'true')
  @IsBoolean()
  excludeCoupled?: boolean;

  /**
   * When `excludeCoupled` is set, locos coupled to THIS train are still kept
   * (they are valid picks — you are editing that train's consist).
   */
  @ApiPropertyOptional({
    description: 'Train id whose own coupled locomotives are NOT excluded',
  })
  @IsOptional()
  @IsUUID()
  excludeTrainId?: string;
}
