import { resolvePaymentGate, type BookingPaymentGateInput } from './booking-payment-gate';

const DAY1 = '2026-09-26';
const DAY2 = '2026-09-27';

const input = (
  paid: number,
  overrides: Partial<BookingPaymentGateInput> = {},
): BookingPaymentGateInput => ({
  is_guaranteed: false,
  paid,
  day_amounts: new Map([
    [DAY1, 100],
    [DAY2, 200],
  ]),
  currency_code: 'AED',
  ...overrides,
});

describe('resolvePaymentGate', () => {
  it('enables day 1 and disables day 2 when paid covers only day 1', () => {
    expect(resolvePaymentGate(input(120), DAY1).payment_pending).toBe(false);
    const day2 = resolvePaymentGate(input(120), DAY2);
    expect(day2.payment_pending).toBe(true);
    expect(day2.required_amount).toBe(300);
    expect(day2.paid_amount).toBe(120);
  });

  it('enables every day when the full day-wise amount is paid', () => {
    expect(resolvePaymentGate(input(300), DAY1).payment_pending).toBe(false);
    expect(resolvePaymentGate(input(300), DAY2).payment_pending).toBe(false);
  });

  it('disables every day when paid is below the day 1 amount', () => {
    expect(resolvePaymentGate(input(50), DAY1).payment_pending).toBe(true);
    expect(resolvePaymentGate(input(50), DAY2).payment_pending).toBe(true);
  });

  it('ignores the overall booking total — only day-wise amounts count', () => {
    // Booking total may be 800, but the day-wise requirement is 300.
    expect(resolvePaymentGate(input(300), DAY2).required_amount).toBe(300);
  });

  it('enables every day on a guaranteed booking regardless of payment', () => {
    const gate = resolvePaymentGate(input(0, { is_guaranteed: true }), DAY2);
    expect(gate.is_guaranteed).toBe(true);
    expect(gate.payment_pending).toBe(false);
  });

  it('treats a zero-amount day as covered', () => {
    const gate = resolvePaymentGate(input(0, { day_amounts: new Map([[DAY1, 0]]) }), DAY1);
    expect(gate.payment_pending).toBe(false);
  });

  it('tolerates sub-cent rounding on the boundary', () => {
    expect(resolvePaymentGate(input(299.999), DAY2).payment_pending).toBe(false);
  });

  it('defaults to unlocked when no gate input exists for the booking', () => {
    expect(resolvePaymentGate(undefined, DAY1).payment_pending).toBe(false);
  });
});
