import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
  ArrayMinSize,
  IsArray,
  IsOptional,
  IsString,
  IsUUID,
  Matches,
  MaxLength,
} from 'class-validator';

export class BuildTrainDto {
  @ApiProperty({ example: '8001', description: 'EXPORT run number (odd, unique across trains)' })
  @IsString()
  @MaxLength(20)
  @Matches(/^\d*[13579]$/, {
    message: 'Export train number must be numeric and odd (e.g. 8001)',
  })
  exportTrainNumber!: string;

  @ApiProperty({ example: '8002', description: 'IMPORT run number (even, unique across trains)' })
  @IsString()
  @MaxLength(20)
  @Matches(/^\d*[02468]$/, {
    message: 'Import train number must be numeric and even (e.g. 8002)',
  })
  importTrainNumber!: string;

  @ApiProperty({ format: 'uuid', description: 'Yard the train is built in' })
  @IsUUID()
  currentYardId!: string;

  @ApiProperty({
    type: [String],
    format: 'uuid',
    description: 'Locomotives pulling the train (minimum 2 — front and back), in consist order',
  })
  @IsArray()
  @ArrayMinSize(2, { message: 'A train must be pulled by at least two locomotives' })
  @IsUUID('all', { each: true })
  locomotiveIds!: string[];

  @ApiPropertyOptional({
    type: [String],
    format: 'uuid',
    description: 'Wagons to attach at build time, in consist order (must sit in the same yard)',
  })
  @IsOptional()
  @IsArray()
  @IsUUID('all', { each: true })
  wagonIds?: string[];

  @ApiPropertyOptional({ maxLength: 100 })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  trainName?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  notes?: string;
}
