import { BadRequestException } from '@nestjs/common';

import { ProcurementService } from './procurement.service';
import { AcquisitionType } from './entities/asset-acquisition.entity';

// PURCHASE acquisitions must not carry lease terms; LEASE/RENTAL may.
describe('ProcurementService acquisition lease-field guard', () => {
  const repo = {
    createAcquisition: jest.fn(async (dto) => dto),
    findAcquisitionById: jest.fn(async () => ({ acquisitionType: AcquisitionType.PURCHASE })),
    updateAcquisition: jest.fn(async (_id, dto) => dto),
  };
  const svc = new ProcurementService(repo as never);

  it('rejects a PURCHASE with lease dates', async () => {
    await expect(
      svc.createAcquisition({
        itemName: 'Brake pads',
        acquisitionType: AcquisitionType.PURCHASE,
        acquisitionDate: '2026-07-22',
        leaseStart: '2026-07-01',
      } as never),
    ).rejects.toThrow(BadRequestException);
  });

  it('accepts a LEASE with lease dates and a plain PURCHASE', async () => {
    await expect(
      svc.createAcquisition({
        itemName: 'Rented crane',
        acquisitionType: AcquisitionType.LEASE,
        acquisitionDate: '2026-07-22',
        leaseStart: '2026-07-01',
        leaseEnd: '2027-07-01',
      } as never),
    ).resolves.toBeDefined();
    await expect(
      svc.createAcquisition({
        itemName: 'Brake pads',
        acquisitionType: AcquisitionType.PURCHASE,
        acquisitionDate: '2026-07-22',
      } as never),
    ).resolves.toBeDefined();
  });

  it('rejects adding lease terms to an acquisition that is a PURCHASE', async () => {
    await expect(
      svc.updateAcquisition('a1', { monthlyPayment: 500 } as never),
    ).rejects.toThrow(BadRequestException);
  });
});
