import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  ArrayMinSize,
  IsArray,
  IsBoolean,
  IsInt,
  IsNumber,
  IsOptional,
  IsString,
  IsUUID,
  Min,
  ValidateNested,
} from 'class-validator';

export class ContainerPlacementDto {
  @ApiProperty({ format: 'uuid' })
  @IsUUID()
  bookingContainerId!: string;

  @ApiProperty({ minimum: 0 })
  @IsInt()
  @Min(0)
  unitIndex!: number;

  @ApiProperty({ minimum: 1 })
  @IsInt()
  @Min(1)
  sequenceNo!: number;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  containerId?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  containerNumber?: string;

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

export class AssignBookingsDto {
  @ApiProperty({ type: [String] })
  @IsArray()
  @ArrayMinSize(1)
  @IsUUID('4', { each: true })
  bookingIds!: string[];

  @ApiPropertyOptional({ description: 'Bypass soft hold and overweight warnings' })
  @IsOptional()
  @IsBoolean()
  forceAssign?: boolean;

  @ApiPropertyOptional({ type: [ContainerPlacementDto] })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ContainerPlacementDto)
  containerPlacements?: ContainerPlacementDto[];

  @ApiPropertyOptional({ description: 'Maximum total booking weight allowed on this train' })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  maxTrainWeightTons?: number;

  @ApiPropertyOptional({ description: 'Maximum total wagon length allowed on this train' })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  maxTrainLengthMeters?: number;

  @ApiPropertyOptional({ description: 'Maximum wagons allowed on this train' })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  maxWagonsPerTrain?: number;
}
