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

export class TruckEntranceDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  ownerName?: string;

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

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

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

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

  @ApiProperty()
  @IsString()
  truckPlateNumber!: string;

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

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

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

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

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

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

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

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

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

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  @Min(0)
  unitCount?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  @Min(0)
  grossWeightKg?: number;

  @ApiPropertyOptional({ description: 'Whether the customer truck was weighed at receipt.' })
  @IsOptional()
  @IsBoolean()
  weighingRequired?: boolean;

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  @Min(0)
  netWeightKg?: number;

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

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

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  @Min(0)
  damagedRejectedQuantity?: number;

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

  @ApiProperty()
  @IsString()
  driverName!: string;

  @ApiProperty()
  @IsString()
  driverPhone!: string;

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

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

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  @Min(0)
  entranceTareWeightKg?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  @Min(0)
  exitTareWeightKg?: number;

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

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

/** Bulk-receive eligible PAID bookings into a warehouse location, by trade direction. */
export class BulkReceiveDto {
  @ApiProperty({ enum: ['IMPORT', 'EXPORT'] })
  @IsIn(['IMPORT', 'EXPORT'])
  direction!: 'IMPORT' | 'EXPORT';

  @ApiProperty({ format: 'uuid' })
  @IsUUID()
  warehouseId!: string;

  @ApiProperty({ format: 'uuid' })
  @IsUUID()
  yardId!: string;

  @ApiProperty({ format: 'uuid' })
  @IsUUID()
  zoneId!: string;

  @ApiProperty({ type: [String], format: 'uuid' })
  @IsArray()
  @ArrayNotEmpty()
  @IsUUID('all', { each: true })
  bookingIds!: string[];

  @ApiPropertyOptional({ type: TruckEntranceDto })
  @IsOptional()
  @ValidateNested()
  @Type(() => TruckEntranceDto)
  truckEntrance?: TruckEntranceDto;

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