import { describe, expect, it } from 'vitest'
import * as orders from './index.js'

describe('@platform-modules/commerce-orders public barrel', () => {
  it('exports every named core symbol', () => {
    for (const name of [
      'OrderChargeConflictError',
      'OrderIdempotencyConflictError',
      'OrderIntegrityError',
      'OrderMigrateError',
      'OrderNotChargeableError',
      'OrderNotFoundError',
      'OrderValidationError',
      'RefundExceedsPaidError',
      'ImmutableFieldError',
      'isOrderChargeConflictError',
      'isOrderIdempotencyConflictError',
      'isOrderIntegrityError',
      'isOrderMigrateError',
      'isOrderNotChargeableError',
      'isOrderNotFoundError',
      'isOrderValidationError',
      'isRefundExceedsPaidError',
      'isImmutableFieldError',
      'claimForCharge',
      'createOrder',
      'failOrder',
      'getOrderById',
      'assertOrderIntegrity',
      'listOrders',
      'markPaid',
      'markUnfulfillable',
      'pushSchema',
      'recordStep',
      'order',
      'orderLine',
      'orderStep',
      'ordersSchema',
      'refundIntent',
      'vendorSplit',
    ]) {
      expect(orders, `missing export: ${name}`).toHaveProperty(name)
      expect((orders as Record<string, unknown>)[name], `${name} is undefined`).toBeDefined()
    }

    expect(typeof orders.createOrder).toBe('function')
    expect(typeof orders.claimForCharge).toBe('function')
    expect(typeof orders.markPaid).toBe('function')
    expect(typeof orders.failOrder).toBe('function')
    expect(typeof orders.markUnfulfillable).toBe('function')
    expect(typeof orders.recordStep).toBe('function')
    expect(typeof orders.getOrderById).toBe('function')
    expect(typeof orders.listOrders).toBe('function')
    expect(typeof orders.assertOrderIntegrity).toBe('function')
    expect(typeof orders.pushSchema).toBe('function')

    expect(
      orders.isOrderChargeConflictError(
        new orders.OrderChargeConflictError({
          orderId: 'x',
          existingChargeRef: 'a',
          attemptedChargeRef: 'b',
        }),
      ),
    ).toBe(true)
    expect(new orders.OrderChargeConflictError({
      orderId: 'x',
      existingChargeRef: 'a',
      attemptedChargeRef: 'b',
    }).httpStatus).toBe(409)
    expect(
      orders.isOrderIdempotencyConflictError(
        new orders.OrderIdempotencyConflictError({ idempotencyKey: 'k' }),
      ),
    ).toBe(true)
    expect(new orders.OrderIdempotencyConflictError({ idempotencyKey: 'k' }).httpStatus).toBe(409)
    expect(orders.isOrderIntegrityError(new orders.OrderIntegrityError('lines'))).toBe(true)
    expect(orders.isOrderNotFoundError(new orders.OrderNotFoundError('x'))).toBe(true)
    expect(orders.isOrderNotChargeableError(new orders.OrderNotChargeableError('x'))).toBe(true)
    expect(orders.isOrderValidationError(new orders.OrderValidationError('x'))).toBe(true)
    expect(
      orders.isRefundExceedsPaidError(
        new orders.RefundExceedsPaidError({
          orderId: 'x',
          requested: 1n,
          alreadyRefunded: 0n,
          paid: 1n,
        }),
      ),
    ).toBe(true)
    expect(orders.isImmutableFieldError(new orders.ImmutableFieldError('orderId'))).toBe(true)
    expect(orders.isOrderMigrateError(new orders.OrderMigrateError('x'))).toBe(true)
  })
})

describe('@platform-modules/commerce-orders/returns subpath', () => {
  it('exports decideReturn and executeRefund from the returns entry', async () => {
    const returns = await import('./returns.js')
    expect(typeof returns.decideReturn).toBe('function')
    expect(typeof returns.executeRefund).toBe('function')
    expect(typeof returns.reconcileStuckRefunds).toBe('function')
  })
})
