import { describe, expect, it } from 'vitest'

describe('revenue forecasting helpers', () => {
  it('discounts overdue committed revenue and only forecasts the outstanding balance', async () => {
    const { buildForecastWindow, summarizeCommittedInvoices } = await import('../../src/queries/forecasting')

    const window = buildForecastWindow(new Date('2026-07-15T00:00:00.000Z'), 3)
    const months = summarizeCommittedInvoices(
      [
        {
          status: 'SENT',
          total: '1000.00',
          amountPaid: '0.00',
          dueDate: '2026-07-20',
          issueDate: '2026-07-01',
        },
        {
          status: 'PARTIALLY_PAID',
          total: '1000.00',
          amountPaid: '250.00',
          dueDate: '2026-07-01',
          issueDate: '2026-06-15',
        },
        {
          status: 'DRAFT',
          total: '9999.00',
          amountPaid: '0.00',
          dueDate: '2026-07-10',
          issueDate: '2026-07-01',
        },
      ],
      window,
      0.8,
    )

    expect(months.get('2026-07')).toBe(1600)
  })

  it('uses reengagement month when present and otherwise spreads weighted lead value across three months', async () => {
    const {
      DEFAULT_LEAD_STAGE_PROBABILITIES,
      buildForecastWindow,
      summarizeProjectedLeads,
    } = await import('../../src/queries/forecasting')

    const window = buildForecastWindow(new Date('2026-07-15T00:00:00.000Z'), 6)
    const months = summarizeProjectedLeads(
      [
        {
          stage: 'QUALIFIED',
          estimatedValue: '900.00',
          reengagementAt: null,
          archivedAt: null,
        },
        {
          stage: 'PROPOSAL',
          estimatedValue: '500.00',
          reengagementAt: '2026-10-05T09:00:00.000Z',
          archivedAt: null,
        },
        {
          stage: 'WON',
          estimatedValue: '1000.00',
          reengagementAt: null,
          archivedAt: null,
        },
      ],
      window,
      DEFAULT_LEAD_STAGE_PROBABILITIES,
    )

    expect(months.get('2026-07')).toBe(90)
    expect(months.get('2026-08')).toBe(90)
    expect(months.get('2026-09')).toBe(90)
    expect(months.get('2026-10')).toBe(300)
    expect(months.has('2026-11')).toBe(false)
  })

  it('expands recurring templates and applies VAT only to taxable lines', async () => {
    const {
      buildForecastWindow,
      summarizeScheduledTemplates,
    } = await import('../../src/queries/forecasting')

    const window = buildForecastWindow(new Date('2026-07-15T00:00:00.000Z'), 3)
    const months = summarizeScheduledTemplates(
      [
        {
          status: 'active',
          nextGenerationDate: '2026-07-16',
          frequency: 'monthly',
          frequencyDay: 16,
          endDate: null,
          vatRate: '0.18',
          lineItems: [
            { quantity: 2, unitPrice: 100, discountPct: 0, taxable: true },
            { quantity: 1, unitPrice: 50, discountPct: 0, taxable: false },
          ],
        },
      ],
      window,
    )

    expect(months.get('2026-07')).toBe(286)
    expect(months.get('2026-08')).toBe(286)
    expect(months.get('2026-09')).toBe(286)
  })
})
