import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsIn, IsNumber, IsOptional, IsString, MaxLength, Min, IsUUID } from 'class-validator';

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

export class CreateLocomotiveDto {
  @ApiProperty({ example: 'LOCO-001' })
  @IsString()
  @MaxLength(32)
  code!: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  @MaxLength(100)
  name?: string;

  @ApiProperty({ enum: LOCOMOTIVE_TYPES })
  @IsIn([...LOCOMOTIVE_TYPES])
  locomotiveType!: string;

  @ApiProperty({ enum: LOCOMOTIVE_STATUSES })
  @IsIn([...LOCOMOTIVE_STATUSES])
  status!: string;

  @ApiPropertyOptional({ description: 'Current yard location' })
  @IsOptional()
  @IsUUID()
  currentYardId?: string;

  @ApiProperty({ example: 3500 })
  @Transform(({ value }) => Number(value))
  @IsNumber()
  @Min(0)
  maxPullWeightTons!: number;

  @ApiProperty({ example: 760 })
  @Transform(({ value }) => Number(value))
  @IsNumber()
  @Min(0)
  maxTrainLengthMeters!: number;

  @ApiPropertyOptional({ example: 4200 })
  @IsOptional()
  @Transform(({ value }) => (value === '' || value == null ? undefined : Number(value)))
  @IsNumber()
  @Min(0)
  powerKw?: number;

  @ApiPropertyOptional({ example: 300 })
  @IsOptional()
  @Transform(({ value }) => (value === '' || value == null ? undefined : Number(value)))
  @IsNumber()
  @Min(0)
  tractionForceKn?: number;

  @ApiPropertyOptional({ example: 120 })
  @IsOptional()
  @Transform(({ value }) => (value === '' || value == null ? undefined : Number(value)))
  @IsNumber()
  @Min(0)
  maxSpeedKmh?: number;
}
