/** Pricing, plan tiers and access rules shared by client and server. */

export type PlanTier = "free" | "pro" | "team";
export type MembershipCategory = "marketplace" | "incubator" | "donor";

export const MEMBERSHIP_CATEGORIES: {
  value: MembershipCategory;
  label: string;
  blurb: string;
}[] = [
  {
    value: "marketplace",
    label: "UZIA Marketplace member (founder / SME)",
    blurb: "Registered on the UZIA Marketplace at uzia.africa.",
  },
  {
    value: "incubator",
    label: "Incubator / Accelerator",
    blurb: "Hubs, incubators and accelerators supporting portfolios of businesses.",
  },
  {
    value: "donor",
    label: "Donor / Development partner",
    blurb: "Funders and programme partners sponsoring cohorts of MSMEs.",
  },
];

export type PlanOption = {
  id: string;
  category: MembershipCategory;
  name: string;
  tier: PlanTier;
  priceKes: number;
  cadence: string;
  seats: number;
  headline: string;
  features: string[];
  contactOnly?: boolean;
};

export const PLANS: PlanOption[] = [
  {
    id: "marketplace-free",
    category: "marketplace",
    name: "Free",
    tier: "free",
    priceKes: 0,
    cadence: "no fee",
    seats: 1,
    headline: "Start with one assessment and every Foundation course.",
    features: [
      "1 assessment and 1 result",
      "All 15 Foundation courses — fully sponsored",
      "Score across the 12 UZIA Academy Pillars",
      "Foundation certificates",
    ],
  },
  {
    id: "marketplace-pro",
    category: "marketplace",
    name: "Pro",
    tier: "pro",
    priceKes: 1500,
    cadence: "one-off",
    seats: 1,
    headline: "Your full action plan plus the whole catalogue.",
    features: [
      "Unlimited assessments and results",
      "Sequenced action plan for every pillar",
      "All Growth, Advanced and Executive courses unlocked",
      "All four credential levels",
    ],
  },
  {
    id: "incubator-pro",
    category: "incubator",
    name: "Pro",
    tier: "pro",
    priceKes: 3000,
    cadence: "one-off",
    seats: 1,
    headline: "Full platform access for one practitioner account.",
    features: [
      "Unlimited assessments and results",
      "Sequenced action plan for every pillar",
      "Entire 50-course catalogue unlocked",
      "All four credential levels",
    ],
  },
  {
    id: "incubator-team",
    category: "incubator",
    name: "Team",
    tier: "team",
    priceKes: 20000,
    cadence: "up to 25 people",
    seats: 25,
    headline: "Run a whole cohort on UZIA Academy.",
    features: [
      "Up to 25 seats under one account",
      "Pro access for every seat",
      "Cohort-wide pillar scores",
      "Certificates for every learner",
    ],
  },
  {
    id: "donor-partner",
    category: "donor",
    name: "Programme partnership",
    tier: "team",
    priceKes: 0,
    cadence: "tailored",
    seats: 0,
    headline: "Sponsor MSME cohorts and track impact.",
    contactOnly: true,
    features: [
      "Sponsored Foundation places at scale",
      "Cohort dashboards and pillar reporting",
      "Custom curriculum pathways",
      "Impact measurement support",
    ],
  },
];

export const PAYMENT_METHODS: { id: string; label: string; instructions: string }[] = [
  {
    id: "mpesa",
    label: "M-Pesa",
    instructions:
      "Go to M-Pesa → Lipa na M-Pesa → Paybill. Business number and account number are shared by UZIA Support on request. Enter the amount, pay, then paste the M-Pesa confirmation code below.",
  },
  {
    id: "airtel",
    label: "Airtel Money",
    instructions:
      "Dial *334# → Make Payments → Pay Bill, use the UZIA Academy merchant details shared by support, pay the amount, then paste the Airtel Money transaction ID below.",
  },
  {
    id: "paypal",
    label: "PayPal",
    instructions:
      "Send the equivalent amount to the UZIA Academy PayPal account shared by support, then paste the PayPal transaction ID below.",
  },
  {
    id: "card",
    label: "Visa / Mastercard",
    instructions:
      "Request a secure card payment link from UZIA Support, complete the payment, then paste the receipt reference below.",
  },
];

export const SUPPORT_EMAIL = "support@uzia.africa";

export function formatKesAmount(amount: number) {
  return `KSh ${amount.toLocaleString("en-KE")}`;
}

/** Foundation courses are fully sponsored — never say "free". */
export function isSponsoredCourse(course: { credential_level?: string | null; price_kes?: number | null }) {
  return course.credential_level === "Foundation" || !course.price_kes;
}

/** Anything above Foundation needs a Pro or Team plan. */
export function requiresPro(course: { credential_level?: string | null }) {
  return course.credential_level !== "Foundation";
}

export function isPaidTier(tier: string | null | undefined) {
  return tier === "pro" || tier === "team";
}

export function courseAccessLabel(course: {
  credential_level?: string | null;
  price_kes?: number | null;
}) {
  return isSponsoredCourse(course) ? "Fully sponsored" : formatKesAmount(course.price_kes ?? 0);
}

export function plansForCategory(category: MembershipCategory) {
  return PLANS.filter((p) => p.category === category);
}
