import { WarehouseInventoryService } from './warehouse-inventory.service';

// Exercises the per-truck [Exit Inspection] block helpers directly (no DI).
const svc = Object.create(WarehouseInventoryService.prototype) as any;

const arrivalA =
  '[Exit Inspection]\nTruck Plate: 3-15288/56858\nDriver: Abebe Lemeno\nGate In Time: 2026-07-21T08:00:00.000Z\nTare Weight: 12 t';
const arrivalB =
  '[Exit Inspection]\nTruck Plate: 3-85957/48562\nDriver: Suleman Tamrat\nGate In Time: 2026-07-21T09:00:00.000Z\nWeighing: SKIPPED';

describe('per-truck exit inspection blocks', () => {
  it('keeps truck A intact when truck B arrives', () => {
    const afterA = svc.replaceExitInspectionNote('Receive note', arrivalA, '3-15288/56858');
    const afterB = svc.replaceExitInspectionNote(afterA, arrivalB, '3-85957/48562');
    expect(afterB).toContain('Abebe Lemeno');
    expect(afterB).toContain('Suleman Tamrat');
    expect(afterB.match(/\[Exit Inspection\]/g)).toHaveLength(2);
    expect(afterB.startsWith('Receive note')).toBe(true);
  });

  it("exit for truck A updates only A's block and preserves arrival data", () => {
    const notes = svc.replaceExitInspectionNote(
      svc.replaceExitInspectionNote(null, arrivalA, '3-15288/56858'),
      arrivalB,
      '3-85957/48562',
    );
    const dto = svc.preserveTruckArrivalForExit(
      { truckPlateNumber: '3-15288/56858', grossWeight: 40, gateOutTime: '2026-07-21T12:00:00.000Z' },
      notes,
    );
    expect(dto.driverName).toBe('Abebe Lemeno');
    expect(dto.tareWeight).toBe(12);
    expect(dto.weighingSkipped).toBeUndefined();
    const exitNote = svc.buildExitInspectionNote(dto);
    const replaced = svc.replaceExitInspectionNote(notes, exitNote, dto.truckPlateNumber);
    expect(replaced).toContain('Gross Weight: 40 t');
    expect(replaced).toContain('Net Weight: 28 t');
    expect(replaced).toContain('Suleman Tamrat'); // B untouched
    expect(replaced.match(/\[Exit Inspection\]/g)).toHaveLength(2);
  });

  it('skipped weighing records the container-derived net in the note', () => {
    const dto = {
      truckPlateNumber: '3-85957/48562',
      driverName: 'Suleman Tamrat',
      weighingSkipped: true,
      netWeight: 27.5,
      gateInTime: '2026-07-21T09:00:00.000Z',
      gateOutTime: '2026-07-21T13:00:00.000Z',
    };
    const note = svc.buildExitInspectionNote(dto);
    expect(note).toContain('Weighing: SKIPPED');
    expect(note).toContain('Net Weight: 27.5 t');
  });

  it('matches a legacy comma-joined plate list and keeps foreign notes', () => {
    const legacy =
      'Receive note\n\n[Exit Inspection]\nTruck Plate: 3-15288/56858, 3-85957/48562\nDriver: Abebe Lemeno\nTare Weight: 12 t\nCUSTOMER_DELIVERY_APPROVAL:{"ok":true}';
    const block = svc.extractExitInspectionForPlate(legacy, '3-15288/56858');
    expect(block).toContain('Abebe Lemeno');
    const replaced = svc.replaceExitInspectionNote(legacy, arrivalA, '3-15288/56858');
    expect(replaced.match(/\[Exit Inspection\]/g)).toHaveLength(1);
    expect(replaced).toContain('CUSTOMER_DELIVERY_APPROVAL:{"ok":true}');
    expect(replaced).toContain('Receive note');
  });
});
