import { cmsApi } from './api.js'

export type PlanTier = 'starter' | 'pro' | 'enterprise'

export type PlanFeature =
  | 'seo.inlineApply'
  | 'seo.contentBrief'
  | 'seo.contentResearch'
  | 'seo.batchContentBriefs'

export interface PlanInfo {
  tier: PlanTier
  features: PlanFeature[]
  upgradeUrl: string | null
}

export async function fetchPlanInfo(): Promise<PlanInfo | null> {
  const res = await cmsApi<PlanInfo>('/plan')
  if (res.error || !res.data) return null
  return res.data
}

/** When plan info is unavailable, assume full access (self-hosted default). */
export function hasPlanFeature(plan: PlanInfo | null | undefined, feature: PlanFeature): boolean {
  if (!plan) return true
  return plan.features.includes(feature)
}

export function planFeatureLabel(feature: PlanFeature): string {
  switch (feature) {
    case 'seo.inlineApply':
      return 'SEO inline apply'
    case 'seo.contentBrief':
      return 'SEO content briefs'
    case 'seo.contentResearch':
      return 'SEO topic research'
    case 'seo.batchContentBriefs':
      return 'SEO batch content briefs'
    default: {
      const _exhaustive: never = feature
      return _exhaustive
    }
  }
}
