import { integer, pgTable, primaryKey, text, timestamp } from 'drizzle-orm/pg-core'

export const entitlements = pgTable(
  'entitlements',
  {
    account: text('account').notNull(),
    capability: text('capability').notNull(),
    tier: text('tier').notNull(),
    grantedAt: timestamp('granted_at', { withTimezone: true }).notNull().defaultNow(),
    expiresAt: timestamp('expires_at', { withTimezone: true }),
  },
  (table) => [
    primaryKey({
      columns: [table.account, table.capability],
      name: 'entitlements_account_capability_pk',
    }),
  ],
)

export const entitlementTierGrants = pgTable(
  'entitlement_tier_grants',
  {
    capability: text('capability').notNull(),
    tier: text('tier').notNull(),
    quotaKey: text('quota_key').notNull().default(''),
    quotaValue: integer('quota_value'),
  },
  (table) => [
    primaryKey({
      columns: [table.capability, table.tier, table.quotaKey],
      name: 'entitlement_tier_grants_capability_tier_quota_pk',
    }),
  ],
)

export const entitlementsSchema = {
  entitlements,
  entitlementTierGrants,
}

export type EntitlementsSchema = typeof entitlementsSchema
