/**
 * usage_counters — foundation-auth-rbac.
 * Metered-entitlement counters keyed by (tenant, counter_key, period).
 * period = 'YYYY-MM' for monthly metrics, 'all_time' for seats.
 */
import { pgTable, uuid, text, integer, primaryKey } from 'drizzle-orm/pg-core'
import { tenants } from './tenants'

export const usageCounters = pgTable(
  'usage_counters',
  {
    tenantId: uuid('tenant_id')
      .notNull()
      .references(() => tenants.id, { onDelete: 'cascade' }),
    counterKey: text('counter_key').notNull(), // e.g. 'ocr_uploads', 'team_seats'
    period: text('period').notNull(), // 'YYYY-MM' or 'all_time'
    count: integer('count').notNull().default(0),
  },
  (t) => ({
    pk: primaryKey({ columns: [t.tenantId, t.counterKey, t.period] }),
  }),
)

export type UsageCounterRow = typeof usageCounters.$inferSelect
