import { PricingContent, type PricingPlan } from "./pricing-content";

interface PlanCatalogResponse {
  plan_key: string;
  name: string;
  description: string;
  price_monthly: number | null;
  price_yearly: number | null;
  cta_label: string;
  cta_href: string;
  features: { label: string; included: boolean }[];
  is_popular: boolean;
}

const fallbackPlans: PricingPlan[] = [
  {
    id: "free",
    name: "Free",
    monthly: 0,
    yearly: 0,
    description: "Try ReelsCutter risk-free",
    cta: "Get Started Free",
    ctaHref: "/signup",
    popular: false,
    features: [
      { label: "1 YouTube video per day", included: true },
      { label: "Max video length: 15 minutes", included: true },
      { label: "Basic reel timestamps", included: true },
      { label: "Virality scores", included: true },
      { label: "Watermarked export", included: true },
      { label: "Priority processing", included: false },
      { label: "Download transcript", included: false },
      { label: "No watermark", included: false },
      { label: "API access", included: false },
      { label: "Dedicated support", included: false },
    ],
    color: "border-white/10",
    badgeColor: "text-zinc-400",
  },
  {
    id: "pro",
    name: "Pro",
    monthly: 799,
    yearly: 6990,
    description: "For serious content creators",
    cta: "Upgrade to Pro",
    ctaHref: "/signup?plan=pro",
    popular: true,
    features: [
      { label: "5 YouTube links per day", included: true },
      { label: "Max video length: 20 minutes", included: true },
      { label: "Advanced reel timestamps", included: true },
      { label: "Virality scores + reasons", included: true },
      { label: "No watermark", included: true },
      { label: "Priority processing", included: true },
      { label: "Download transcript", included: true },
      { label: "Email support", included: true },
      { label: "API access", included: false },
      { label: "Dedicated support", included: false },
    ],
    color: "border-violet-500/50",
    badgeColor: "text-violet-300",
    glow: true,
  },
  {
    id: "pro_max",
    name: "Pro Max",
    monthly: null,
    yearly: null,
    description: "For agencies & power users",
    cta: "Contact Us",
    ctaHref: "/contact",
    popular: false,
    features: [
      { label: "Unlimited YouTube links", included: true },
      { label: "Unlimited video length", included: true },
      { label: "Advanced reel timestamps", included: true },
      { label: "Full virality analysis", included: true },
      { label: "No watermark", included: true },
      { label: "Priority processing", included: true },
      { label: "Download transcript", included: true },
      { label: "API access", included: true },
      { label: "Custom integrations", included: true },
      { label: "Dedicated account manager", included: true },
    ],
    color: "border-cyan-500/30",
    badgeColor: "text-cyan-300",
  },
];

function planColor(planKey: string) {
  if (planKey === "pro") return { color: "border-violet-500/50", badgeColor: "text-violet-300", glow: true };
  if (planKey === "pro_max") return { color: "border-cyan-500/30", badgeColor: "text-cyan-300", glow: false };
  return { color: "border-white/10", badgeColor: "text-zinc-400", glow: false };
}

function toPricingPlan(plan: PlanCatalogResponse): PricingPlan {
  return {
    id: plan.plan_key,
    name: plan.name,
    monthly: plan.price_monthly,
    yearly: plan.price_yearly,
    description: plan.description,
    cta: plan.cta_label,
    ctaHref: plan.cta_href,
    popular: plan.is_popular,
    features: plan.features,
    ...planColor(plan.plan_key),
  };
}

async function getPlans(): Promise<PricingPlan[]> {
  const base = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:8000";

  try {
    const response = await fetch(`${base}/plans`, { cache: "no-store" });
    if (!response.ok) return fallbackPlans;

    const data = (await response.json()) as PlanCatalogResponse[];
    return data.length > 0 ? data.map(toPricingPlan) : fallbackPlans;
  } catch {
    return fallbackPlans;
  }
}

export default async function PricingPage() {
  const plans = await getPlans();

  return <PricingContent plans={plans} />;
}
