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

export class CreateBookingOrderLineDto {
  @ApiPropertyOptional({
    format: 'uuid',
    description: 'Container type for this line (CONTAINER contracts). Omit for bulk/break-bulk.',
  })
  @IsOptional()
  @IsUUID()
  containerTypeId?: string;

  @ApiProperty({ description: 'Quantity to draw down (containers, tons, or items)', minimum: 0 })
  @IsNumber()
  @Min(0)
  @Transform(({ value }) => Number(value))
  quantity!: number;

  @ApiPropertyOptional({
    description: 'How much of this line is hazardous (≤ quantity). Defaults to 0.',
    minimum: 0,
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  @Transform(({ value }) => Number(value ?? 0))
  hazardousQuantity?: number = 0;

  @ApiPropertyOptional({
    description: 'How much of this line is refrigerated (≤ quantity). Defaults to 0.',
    minimum: 0,
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  @Transform(({ value }) => Number(value ?? 0))
  reeferQuantity?: number = 0;
}

export class CreateBookingOrderDto {
  @ApiProperty({ format: 'uuid', description: 'The general contract to draw down from' })
  @IsUUID()
  contractBookingId!: string;

  @ApiPropertyOptional({
    format: 'uuid',
    description:
      'For multi-route contracts: the contract route line being drawn from. ' +
      'Determines the shipment origin/destination. Omit for single-route contracts.',
  })
  @IsOptional()
  @IsUUID()
  routeLineId?: string;

  @ApiProperty({ example: '2026-07-01T00:00:00.000Z', description: 'Shipment day for this order' })
  @IsDateString()
  scheduledDate!: string;

  @ApiProperty({ type: [CreateBookingOrderLineDto] })
  @IsArray()
  @ArrayMinSize(1)
  @ValidateNested({ each: true })
  @Type(() => CreateBookingOrderLineDto)
  lines!: CreateBookingOrderLineDto[];
}
