import { describe, expect, it } from 'vitest'
import {
  commit,
  createMetering,
  expireStale,
  meteringSchema,
  pushSchema,
  rate,
  release,
  reserve,
  resolve,
  schema,
  usage,
} from './index.js'
import type {
  EntitlementPolicy,
  MeteringDatabase,
  MeteringService,
  PolicyStore,
  SubjectRef,
} from './index.js'

const subject: SubjectRef = { tenantId: 'tenant-1', account: 'account-1' }
const policy: EntitlementPolicy = {
  meter: 'api.calls',
  includedUnits: 100n,
  overage: 'deny',
  period: {
    id: 'period-1',
    startsAt: new Date('2026-07-01T00:00:00.000Z'),
    endsAt: new Date('2026-08-01T00:00:00.000Z'),
  },
  version: 'policy-1',
}

describe('metering barrel', () => {
  it('re-exports schema and every standalone operation', () => {
    expect(schema).toBe(meteringSchema)
    expect(pushSchema).toBeTypeOf('function')
    expect(resolve).toBeTypeOf('function')
    expect(reserve).toBeTypeOf('function')
    expect(commit).toBeTypeOf('function')
    expect(release).toBeTypeOf('function')
    expect(expireStale).toBeTypeOf('function')
    expect(usage).toBeTypeOf('function')
    expect(rate).toBeTypeOf('function')
  })

  it('creates a service that binds the policy store to resolve', async () => {
    const policies: PolicyStore = {
      resolve: async () => policy,
    }
    const service: MeteringService = createMetering({
      db: {} as MeteringDatabase,
      policies,
    })

    await expect(service.resolve(subject, policy.meter, new Date('2026-07-15T00:00:00.000Z'))).resolves.toEqual(policy)
  })
})
