// src/lib/billing/plans.ts — single source of truth for the plan grid.
// Pure data + tiny helpers (no I/O) — consumed by the auth plugin config AND
// the UI (billing page, wizard, blocking page). Pricing source of truth:
// docs/superpowers/specs/2026-06-12-stripe-billing-core-design.md (Pricing.md).
// Annual prices are the marketing "-20 %" grid (rounded to marketing prices,
// e.g. pro $129 → $99/mo billed yearly), so the discount is AT LEAST 20 %.

export type PlanName = "solo" | "starter" | "pro" | "agency";

export interface PlanLimits {
  keywords: number;
  projects: number;
  seats: number;
  integrations: number;
  backlinkRows: number;
  mcpMonthly: number; // -1 = unlimited
}

export interface PlanDef {
  name: PlanName;
  lookupKey: string; // monthly
  annualLookupKey: string; // annual
  monthlyUsd: number; // cents/month
  annualUsd: number; // cents/year
  limits: PlanLimits;
  seatLookupKey?: string; // "seat_monthly" — pro & agency only
  seatAnnualLookupKey?: string; // "seat_annual"
}

/** App-level trial length (no card, stamped on the user's first org). */
export const TRIAL_DAYS = 14;
/** Extra seat (pro/agency), cents/month. */
export const SEAT_MONTHLY_USD = 1900;
/** Extra seat (pro/agency), cents/year (19 × 12 × 0.8). */
export const SEAT_ANNUAL_USD = 18240;

export const PLANS: PlanDef[] = [
  {
    name: "solo",
    lookupKey: "solo_monthly",
    annualLookupKey: "solo_annual",
    monthlyUsd: 999, // $9.99
    annualUsd: 9588, // $95.88/yr ($7.99/mo)
    limits: {
      keywords: 100,
      projects: 1,
      seats: 1,
      integrations: 1,
      backlinkRows: 10_000,
      mcpMonthly: 1_000,
    },
  },
  {
    name: "starter",
    lookupKey: "starter_monthly",
    annualLookupKey: "starter_annual",
    monthlyUsd: 4900, // $49
    annualUsd: 46800, // $468/yr ($39/mo)
    limits: {
      keywords: 500,
      projects: 5,
      seats: 1,
      integrations: 2,
      backlinkRows: 100_000,
      mcpMonthly: 10_000,
    },
  },
  {
    name: "pro",
    lookupKey: "pro_monthly",
    annualLookupKey: "pro_annual",
    monthlyUsd: 12900, // $129
    annualUsd: 118800, // $1 188/yr ($99/mo)
    limits: {
      keywords: 1_500,
      projects: 15,
      seats: 3,
      integrations: 5,
      backlinkRows: 500_000,
      mcpMonthly: 100_000,
    },
    seatLookupKey: "seat_monthly",
    seatAnnualLookupKey: "seat_annual",
  },
  {
    name: "agency",
    lookupKey: "agency_monthly",
    annualLookupKey: "agency_annual",
    monthlyUsd: 29900, // $299
    annualUsd: 286800, // $2 868/yr ($239/mo)
    limits: {
      keywords: 5_000,
      projects: 40,
      seats: 10,
      integrations: -1, // unlimited
      backlinkRows: 2_000_000,
      mcpMonthly: 500_000,
    },
    seatLookupKey: "seat_monthly",
    seatAnnualLookupKey: "seat_annual",
  },
];

export function getPlan(name: PlanName): PlanDef {
  const plan = PLANS.find((p) => p.name === name);
  if (!plan) throw new Error(`Unknown plan: ${name}`);
  return plan;
}

/** Tier index of a plan in PLANS (0 = lowest, higher = pricier). Returns -1 for
 *  no plan or an unknown name. Use to compare tiers — e.g. hide an
 *  "Upgrade to Pro" upsell once the org is already on Pro or above. */
export function planRank(plan: string | null | undefined): number {
  return plan ? PLANS.findIndex((p) => p.name === plan) : -1;
}
