import 'reflect-metadata';
import { plainToInstance } from 'class-transformer';
import { CreateBookingDto } from './create-booking.dto';

/**
 * Boolean flags arrive as STRINGS over multipart/form-data ("true" / "false").
 * The global freight ValidationPipe runs with enableImplicitConversion = false,
 * so only the explicit @Transform on each flag coerces it. This pins that the
 * literal string "false" maps to boolean `false` — class-transformer's implicit
 * boolean coercion would otherwise turn any non-empty string (including "false")
 * into `true`, silently flagging non-hazardous bookings as hazardous.
 */
describe('CreateBookingDto — boolean coercion from multipart strings', () => {
  // Mirror the production pipe: explicit transforms only, no implicit coercion.
  const toDto = (plain: Record<string, unknown>) =>
    plainToInstance(CreateBookingDto, plain, {
      enableImplicitConversion: false,
    }) as unknown as CreateBookingDto;

  it('maps the string "false" to boolean false for every flag', () => {
    const dto = toDto({
      isHazardous: 'false',
      isGovernment: 'false',
      customsClearingEnabled: 'false',
    });
    expect(dto.isHazardous).toBe(false);
    expect(dto.isGovernment).toBe(false);
    expect(dto.customsClearingEnabled).toBe(false);
  });

  it('maps the string "true" to boolean true for every flag', () => {
    const dto = toDto({
      isHazardous: 'true',
      isGovernment: 'true',
      customsClearingEnabled: 'true',
    });
    expect(dto.isHazardous).toBe(true);
    expect(dto.isGovernment).toBe(true);
    expect(dto.customsClearingEnabled).toBe(true);
  });

  it('still coerces numeric form strings to numbers', () => {
    const dto = toDto({ cargoTotalWeightVgm: '12.5' });
    expect(dto.cargoTotalWeightVgm).toBe(12.5);
    expect(typeof dto.cargoTotalWeightVgm).toBe('number');
  });
});
