"use client";

import { useEffect, useState } from "react";
import { AppShell } from "../../../components/app-shell";
import { apiFetch } from "@/lib/api";

type ApiResponse<T> = {
  data: T;
  meta: { timestamp: string; version: "v1" };
  error: null | { message: string; code?: string };
};

type SubscriptionData = {
  plan?: string;
  status?: string;
  billingCycleAnchor?: number;
  cancelAtPeriodEnd?: boolean;
};

const PLAN_LABELS: Record<string, string> = {
  free: "Free",
  pro: "Pro",
  elite: "Elite",
};

const PLAN_DESCRIPTIONS: Record<string, string> = {
  free: "Up to 2 accounts, basic CSV export, 30-day history.",
  pro: "Unlimited accounts, Google Sheets, 24-month history, priority support.",
  elite: "Everything in Pro + tax return module, AI rule suggestions, dedicated support.",
};

export default function SubscriptionPage() {
  const [sub, setSub] = useState<SubscriptionData | null>(null);
  const [loading, setLoading] = useState(true);
  const [actionStatus, setActionStatus] = useState("");
  const [actionLoading, setActionLoading] = useState(false);

  useEffect(() => {
    apiFetch<SubscriptionData>("/api/stripe/subscription")
      .then((res) => {
        if (!res.error) setSub(res.data);
      })
      .catch(() => {})
      .finally(() => setLoading(false));
  }, []);

  const handleUpgrade = async (plan: string) => {
    setActionLoading(true);
    setActionStatus("Redirecting to checkout...");
    const appUrl = typeof window !== "undefined" ? window.location.origin : "";
    const res = await apiFetch<{ url: string }>("/api/stripe/checkout", {
      method: "POST",
      body: JSON.stringify({
        plan,
        successUrl: `${appUrl}/settings/subscription?upgraded=1`,
        cancelUrl: `${appUrl}/settings/subscription`,
      }),
    });
    setActionLoading(false);
    if (res.error || !res.data?.url) {
      setActionStatus(res.error?.message ?? "Could not start checkout. Check Stripe configuration.");
      return;
    }
    window.location.href = res.data.url;
  };

  const handlePortal = async () => {
    setActionLoading(true);
    setActionStatus("Redirecting to billing portal...");
    const appUrl = typeof window !== "undefined" ? window.location.origin : "";
    const res = await apiFetch<{ url: string }>("/api/stripe/portal", {
      method: "POST",
      body: JSON.stringify({ returnUrl: `${appUrl}/settings/subscription` }),
    });
    setActionLoading(false);
    if (res.error || !res.data?.url) {
      setActionStatus(res.error?.message ?? "Could not open billing portal. Check Stripe configuration.");
      return;
    }
    window.location.href = res.data.url;
  };

  const currentPlan = sub?.plan ?? "free";
  const planLabel = PLAN_LABELS[currentPlan] ?? currentPlan;
  const planDesc = PLAN_DESCRIPTIONS[currentPlan] ?? "";

  return (
    <AppShell title="Subscription" subtitle="Manage your plan and billing details.">
      <div className="max-w-2xl space-y-6">
        {/* Current plan card */}
        <div className="glass-panel rounded-2xl p-8">
          <p className="text-xs uppercase tracking-[0.2em] text-muted-foreground font-semibold">Current Plan</p>
          {loading ? (
            <div className="mt-4 h-8 w-32 bg-secondary/60 rounded animate-pulse" />
          ) : (
            <>
              <div className="mt-3 flex items-center gap-3">
                <span className="text-3xl font-bold text-foreground">{planLabel}</span>
                {sub?.status && sub.status !== "active" && sub.status !== "free" && (
                  <span className="text-xs font-medium px-2 py-1 rounded-full bg-yellow-500/10 text-yellow-500 capitalize">
                    {sub.status}
                  </span>
                )}
                {(sub?.status === "active" || currentPlan !== "free") && (
                  <span className="text-xs font-medium px-2 py-1 rounded-full bg-primary/10 text-primary">Active</span>
                )}
              </div>
              <p className="mt-2 text-sm text-muted-foreground">{planDesc}</p>
              {sub?.cancelAtPeriodEnd && (
                <p className="mt-2 text-xs text-yellow-500">Cancels at end of billing period.</p>
              )}
            </>
          )}

          {!loading && currentPlan !== "free" && (
            <button
              onClick={handlePortal}
              disabled={actionLoading}
              className="mt-6 rounded-lg border border-border bg-secondary/30 py-2 px-4 text-sm font-medium text-foreground hover:bg-secondary/60 transition-all disabled:opacity-50"
            >
              Manage Billing
            </button>
          )}
        </div>

        {/* Upgrade options */}
        {currentPlan === "free" && (
          <div className="grid gap-4 md:grid-cols-2">
            {(["pro", "elite"] as const).map((plan) => (
              <div key={plan} className="glass-panel rounded-2xl p-6 border border-border hover:border-primary/50 transition-all">
                <p className="text-lg font-bold text-foreground capitalize">{plan}</p>
                <p className="mt-1 text-sm text-muted-foreground">{PLAN_DESCRIPTIONS[plan]}</p>
                <button
                  onClick={() => handleUpgrade(plan)}
                  disabled={actionLoading}
                  className="mt-4 w-full rounded-lg bg-primary py-2 px-4 text-sm font-bold text-primary-foreground hover:bg-primary/90 transition-all disabled:opacity-50"
                >
                  Upgrade to {PLAN_LABELS[plan]}
                </button>
              </div>
            ))}
          </div>
        )}

        {currentPlan === "pro" && (
          <div className="glass-panel rounded-2xl p-6 border border-border hover:border-primary/50 transition-all">
            <p className="text-lg font-bold text-foreground">Elite</p>
            <p className="mt-1 text-sm text-muted-foreground">{PLAN_DESCRIPTIONS.elite}</p>
            <button
              onClick={() => handleUpgrade("elite")}
              disabled={actionLoading}
              className="mt-4 rounded-lg bg-primary py-2 px-4 text-sm font-bold text-primary-foreground hover:bg-primary/90 transition-all disabled:opacity-50"
            >
              Upgrade to Elite
            </button>
          </div>
        )}

        {actionStatus && (
          <p className="text-sm text-muted-foreground">{actionStatus}</p>
        )}
      </div>
    </AppShell>
  );
}
