"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { ArrowRight, Shield } from "lucide-react";

import { PackageCard } from "@/components/pricing/package-card";
import { Button } from "@/components/ui/button";
import { PACKAGES } from "@/data/packages";
import type { PackageTier } from "@/types/novalife";

export default function UnlockPage() {
  const router = useRouter();
  const [selectedTier, setSelectedTier] = useState<PackageTier>("advanced");

  const selectedPkg = PACKAGES.find((p) => p.id === selectedTier);

  const handleContinue = () => {
    router.push(`/result/checkout?package=${selectedTier}`);
  };

  return (
    <div className="min-h-screen bg-gray-50 py-12 px-4">
      <div className="max-w-5xl mx-auto">
        {/* Header */}
        <div className="text-center mb-10">
          <h1 className="text-3xl sm:text-4xl font-extrabold text-gray-900 mb-3">
            Choose Your Report Package
          </h1>
          <p className="text-lg text-gray-500 max-w-xl mx-auto">
            Unlock the level of insight you need — all plans include your personalised archetype
            analysis
          </p>
        </div>

        {/* Package cards */}
        <div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
          {PACKAGES.map((pkg) => (
            <PackageCard
              key={pkg.id}
              pkg={pkg}
              isSelected={selectedTier === pkg.id}
              onSelect={() => setSelectedTier(pkg.id)}
              showCta={false}
            />
          ))}
        </div>

        {/* Continue button */}
        <div className="text-center space-y-3">
          <Button
            onClick={handleContinue}
            className="bg-teal-600 hover:bg-teal-700 text-white font-bold px-8 py-3 rounded-xl text-lg shadow-md hover:shadow-lg transition-all group"
          >
            Continue with {selectedPkg?.name} — CHF {selectedPkg?.priceChf}
            <ArrowRight className="ml-2 h-5 w-5 group-hover:translate-x-0.5 transition-transform" />
          </Button>

          <div className="flex items-center justify-center gap-1.5 text-xs text-gray-400">
            <Shield className="h-3.5 w-3.5 text-teal-600" />
            <span>Secure checkout powered by Stripe — one-time payment, instant access</span>
          </div>
        </div>

        {/* Comparison table note */}
        <div className="mt-12 bg-white rounded-2xl border border-gray-100 p-6 shadow-sm">
          <h2 className="text-lg font-bold text-gray-900 mb-4 text-center">
            What's included per tier
          </h2>
          <div className="overflow-x-auto">
            <table className="w-full text-sm">
              <thead>
                <tr className="border-b border-gray-100">
                  <th className="text-left py-2 pr-4 text-gray-500 font-medium">Feature</th>
                  {PACKAGES.map((pkg) => (
                    <th key={pkg.id} className="text-center py-2 px-3 font-semibold text-gray-800">
                      {pkg.name}
                    </th>
                  ))}
                </tr>
              </thead>
              <tbody>
                {[
                  { feature: "Archetype profile & traits", starter: true, advanced: true, professional: true },
                  { feature: "BMI & metabolic snapshot", starter: true, advanced: true, professional: true },
                  { feature: "Root-cause analysis", starter: false, advanced: true, professional: true },
                  { feature: "Full nutrition strategy", starter: false, advanced: true, professional: true },
                  { feature: "Lifestyle protocol", starter: false, advanced: true, professional: true },
                  { feature: "GLP-1 assessment", starter: false, advanced: true, professional: true },
                  { feature: "30-day action plan", starter: false, advanced: true, professional: true },
                  { feature: "90-day action plan", starter: false, advanced: false, professional: true },
                  { feature: "Monthly coaching check-ins", starter: false, advanced: false, professional: true },
                  { feature: "Direct coach access", starter: false, advanced: false, professional: true },
                ].map((row) => (
                  <tr key={row.feature} className="border-b border-gray-50 last:border-0">
                    <td className="py-2.5 pr-4 text-gray-600">{row.feature}</td>
                    {(["starter", "advanced", "professional"] as const).map((tier) => (
                      <td key={tier} className="text-center py-2.5 px-3">
                        {row[tier] ? (
                          <span className="inline-flex h-5 w-5 items-center justify-center rounded-full bg-teal-100 text-teal-700 mx-auto">
                            <svg className="h-3 w-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={3}>
                              <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
                            </svg>
                          </span>
                        ) : (
                          <span className="inline-flex h-5 w-5 items-center justify-center text-gray-300 mx-auto text-lg leading-none">
                            —
                          </span>
                        )}
                      </td>
                    ))}
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  );
}
