import { describe, expect, it } from 'vitest'
import { buyerRefundSummary, participantRefundLabel, purchaseStatusAfterRefund, refundReportIssues, type RefundReport } from '@/lib/domain/refund-summary'

const items = [
  { itemId: 'tx', lineTotalCents: 4500, refundedCents: 0 },
  { itemId: 'ok', lineTotalCents: 3000, refundedCents: 0 },
  { itemId: 'ar', lineTotalCents: 2500, refundedCents: 0 },
]
const total = 10_000

describe('refund reports (S7)', () => {
  it('accepts a partial refund of one whole item', () => {
    const report: RefundReport = {
      purchaseStatus: 'partially_refunded', refundedCents: 3000, remainingCents: 7000,
      items: items.map((i) => (i.itemId === 'ok' ? { ...i, refundedCents: 3000 } : i)),
    }
    expect(refundReportIssues(total, report)).toEqual([])
    expect(buyerRefundSummary(total, report)).toEqual({
      label: 'Partially refunded', originalCents: 10_000, refundedCents: 3000, remainingCents: 7000, refundedItemIds: ['ok'],
    })
    expect(participantRefundLabel(report)).toBe('Partially refunded')
  })

  it('accepts a full refund', () => {
    const report: RefundReport = {
      purchaseStatus: 'refunded', refundedCents: total, remainingCents: 0,
      items: items.map((i) => ({ ...i, refundedCents: i.lineTotalCents })),
    }
    expect(refundReportIssues(total, report)).toEqual([])
    expect(buyerRefundSummary(total, report).label).toBe('Refunded')
  })

  it('refuses amounts that do not add up to the purchase total', () => {
    const report: RefundReport = { purchaseStatus: 'partially_refunded', refundedCents: 3000, remainingCents: 6000, items: [] }
    expect(refundReportIssues(total, report)).toContain('amounts_do_not_add_up')
  })

  it('refuses a status that contradicts the amounts', () => {
    expect(refundReportIssues(total, { purchaseStatus: 'refunded', refundedCents: 3000, remainingCents: 7000, items: [] }))
      .toContain('status_does_not_match_amounts')
    expect(refundReportIssues(total, { purchaseStatus: 'partially_refunded', refundedCents: 0, remainingCents: total, items: [] }))
      .toContain('status_does_not_match_amounts')
  })

  it('refuses a partly refunded item (items are refunded whole)', () => {
    const report: RefundReport = {
      purchaseStatus: 'partially_refunded', refundedCents: 1000, remainingCents: 9000,
      items: items.map((i) => (i.itemId === 'tx' ? { ...i, refundedCents: 1000 } : i)),
    }
    expect(refundReportIssues(total, report)).toContain('item_partly_refunded')
  })

  it('refuses item refunds that differ from the purchase refund, unknown items and negative amounts', () => {
    const report: RefundReport = {
      purchaseStatus: 'partially_refunded', refundedCents: 4500, remainingCents: 5500,
      items: [{ itemId: 'zz', lineTotalCents: 3000, refundedCents: 3000 }],
    }
    const issues = refundReportIssues(total, report, ['tx', 'ok', 'ar'])
    expect(issues).toContain('item_refunds_do_not_match_total')
    expect(issues).toContain('unknown_item')
    expect(refundReportIssues(total, { purchaseStatus: 'paid', refundedCents: -1, remainingCents: total + 1, items: [] }))
      .toContain('negative_amount')
  })

  it('never moves a purchase backwards when an older report arrives late', () => {
    expect(purchaseStatusAfterRefund('paid', 'partially_refunded')).toBe('partially_refunded')
    expect(purchaseStatusAfterRefund('partially_refunded', 'refunded')).toBe('refunded')
    expect(purchaseStatusAfterRefund('refunded', 'partially_refunded')).toBeNull()
    expect(purchaseStatusAfterRefund('partially_refunded', 'paid')).toBeNull()
    expect(purchaseStatusAfterRefund('pending_payment', 'refunded')).toBeNull()
  })
})
