import {
  currencyRounding,
  destinationCurrencyId,
  resolveDestinationTotal,
  roundToStep,
  toDestinationAmount,
} from './destination-total';

describe('resolveDestinationTotal', () => {
  const base = { destCurrencyId: 'AED', exchangeRate: 0.272972, rounding: 1 };

  it('USD 173 with the panel total AED 635 → 635 (pre-rounding total kept)', () => {
    expect(
      resolveDestinationTotal({
        ...base,
        sellingTotal: 173,
        sellingCurrencyId: 'USD',
        clientDestTotal: 635,
      }),
    ).toBe(635);
  });

  it('rejects a client total that does not match the selling price', () => {
    expect(
      resolveDestinationTotal({
        ...base,
        sellingTotal: 173,
        sellingCurrencyId: 'USD',
        clientDestTotal: 900,
      }),
    ).toBe(633.76);
  });

  it('no client total → selling ÷ rate', () => {
    expect(resolveDestinationTotal({ ...base, sellingTotal: 173, sellingCurrencyId: 'USD' })).toBe(
      633.76,
    );
  });

  it('INR with rounding 100 accepts the pre-rounding total inside the step', () => {
    // 635 × 24.17 = 15,347.95 → rounded to 15,300 (step 100)
    expect(
      resolveDestinationTotal({
        destCurrencyId: 'AED',
        exchangeRate: 24.17,
        rounding: 100,
        sellingTotal: 15300,
        sellingCurrencyId: 'INR',
        clientDestTotal: 635,
      }),
    ).toBe(635);
  });

  it('billed in the destination currency → the selling total itself', () => {
    expect(
      resolveDestinationTotal({
        ...base,
        exchangeRate: 1,
        sellingTotal: 635,
        sellingCurrencyId: 'AED',
        clientDestTotal: 634.6,
      }),
    ).toBe(635);
  });

  it('destination without a currency → the selling total', () => {
    expect(
      resolveDestinationTotal({
        ...base,
        destCurrencyId: null,
        sellingTotal: 173,
        sellingCurrencyId: 'USD',
      }),
    ).toBe(173);
  });
});

describe('toDestinationAmount', () => {
  it('converts a selling amount at the rate', () => {
    expect(toDestinationAmount(27.3, 'USD', 'AED', 0.273)).toBe(100);
  });
  it('same currency is unchanged', () => {
    expect(toDestinationAmount(100, 'AED', 'AED', 0.5)).toBe(100);
  });
});

describe('destinationCurrencyId', () => {
  it('filters by tenant', async () => {
    const query = jest.fn().mockResolvedValue([{ currency_id: 'AED' }]);
    await expect(destinationCurrencyId({ query }, 'tenant-A', 'dubai')).resolves.toBe('AED');
    expect(query.mock.calls[0][0]).toMatch(/tenant_id = \$2/);
    expect(query.mock.calls[0][1]).toEqual(['dubai', 'tenant-A']);
  });
  it("another tenant's destination is not found", async () => {
    const query = jest.fn().mockResolvedValue([]);
    await expect(destinationCurrencyId({ query }, 'tenant-B', 'dubai')).resolves.toBeNull();
  });
});

describe('AED bill rounded with the destination currency step (client 2026-11-07)', () => {
  it('MT-705308: panel AED 10,714.75 with AED rounding 1 → 10,715', () => {
    expect(
      resolveDestinationTotal({
        sellingTotal: 2918,
        sellingCurrencyId: 'USD',
        destCurrencyId: 'AED',
        exchangeRate: 0.272294,
        rounding: 1,
        clientDestTotal: 10714.75,
        destRounding: 1,
      }),
    ).toBe(10715);
  });

  it('fallback (no panel total) is rounded too', () => {
    expect(
      resolveDestinationTotal({
        sellingTotal: 2918,
        sellingCurrencyId: 'USD',
        destCurrencyId: 'AED',
        exchangeRate: 0.272294,
        rounding: 1,
        destRounding: 1,
      }),
    ).toBe(10716);
  });

  it('INR step 100 on the selling side does not round AED to 100', () => {
    expect(
      resolveDestinationTotal({
        destCurrencyId: 'AED',
        exchangeRate: 24.17,
        rounding: 100,
        sellingTotal: 15300,
        sellingCurrencyId: 'INR',
        clientDestTotal: 634.6,
        destRounding: 1,
      }),
    ).toBe(635);
  });

  it('no destination step → 2 decimals, as before', () => {
    expect(roundToStep(10714.75, null)).toBe(10714.75);
    expect(roundToStep(10714.75, 1)).toBe(10715);
    expect(roundToStep(10714.25, 0.5)).toBe(10714.5);
  });

  it('currencyRounding reads the step tenant-scoped', async () => {
    const query = jest.fn().mockResolvedValue([{ rounding: '1.00' }]);
    expect(await currencyRounding({ query }, 'tenant-1', 'aed')).toBe(1);
    expect(query.mock.calls[0][0]).toContain('tenant_id = $2');
    expect(query.mock.calls[0][1]).toEqual(['aed', 'tenant-1']);
    expect(await currencyRounding({ query }, 'tenant-1', null)).toBeNull();
  });
});
