/**
 * Static tier-quota config — foundation-auth-rbac.
 * `getQuotaLimit(tier, key)` returns a numeric cap (Infinity = unlimited).
 * Static map (NOT DB) so changing a tier limit needs no schema migration.
 *
 * `team_seats` mirrors `getMaxTeamMembers` in @zync/auth (1/8/15/50). The values
 * are duplicated here intentionally to avoid a packages/db -> packages/auth
 * dependency edge; both lock to the spec's entitlement matrix.
 */
import { TenantTier } from '@zync/types'

const TEAM_SEATS: Record<TenantTier, number> = {
  [TenantTier.FREELANCER]: 1,
  [TenantTier.BUSINESS]: 8,
  [TenantTier.ENTERPRISE]: 15,
  [TenantTier.WHITE_LABEL]: 50,
}

const OCR_UPLOADS: Record<TenantTier, number> = {
  [TenantTier.FREELANCER]: 50,
  [TenantTier.BUSINESS]: Infinity,
  [TenantTier.ENTERPRISE]: Infinity,
  [TenantTier.WHITE_LABEL]: Infinity,
}

const SCHEDULES: Record<TenantTier, number> = {
  [TenantTier.FREELANCER]: 1,
  [TenantTier.BUSINESS]: Infinity,
  [TenantTier.ENTERPRISE]: Infinity,
  [TenantTier.WHITE_LABEL]: Infinity,
}

const CALENDARS: Record<TenantTier, number> = {
  [TenantTier.FREELANCER]: 1,
  [TenantTier.BUSINESS]: Infinity,
  [TenantTier.ENTERPRISE]: Infinity,
  [TenantTier.WHITE_LABEL]: Infinity,
}

const QUOTA_MAP: Record<string, Record<TenantTier, number>> = {
  team_seats: TEAM_SEATS,
  ocr_uploads: OCR_UPLOADS,
  schedules: SCHEDULES,
  calendars: CALENDARS,
}

/**
 * Resolve the numeric quota for (tier, counterKey). Unknown keys are treated as
 * unlimited (Infinity) so a not-yet-configured metered feature never hard-blocks
 * before its owning spec wires a limit in.
 */
export function getQuotaLimit(tier: TenantTier, key: string): number {
  const byTier = QUOTA_MAP[key]
  if (!byTier) return Infinity
  return byTier[tier] ?? Infinity
}
