"use client";

import React, { useState } from "react";
import Link from "next/link";
import { Check, ArrowRight, Sparkles } from "lucide-react";
import { AsciiGridBackground } from "./AsciiGridBackground";
import { BlinkingSquares } from "./BlinkingSquares";
import { useAppStore } from "@/lib/store";

export function PackagesSection() {
  const { services, packages, formatPrice, t } = useAppStore();
  const [selectedServiceSlug, setSelectedServiceSlug] = useState<string>("web-development");

  // Get packages for current active service
  const currentPackages = packages.filter((p) => p.service_slug === selectedServiceSlug);
  const starterPkg = currentPackages[0] || packages[0];
  const popularPkg = currentPackages[1] || packages[1];
  const enterprisePkg = currentPackages[2] || packages[2];

  return (
    <div className="space-y-10">
      {/* Specialized Service Tabs Selector */}
      <div className="flex flex-wrap items-center justify-center gap-2 p-1.5 rounded-full border border-border bg-card max-w-4xl mx-auto shadow-xs">
        {services.map((srv) => {
          const isSelected = selectedServiceSlug === srv.slug;
          return (
            <button
              key={srv.id}
              onClick={() => setSelectedServiceSlug(srv.slug)}
              className={`rounded-full px-4 py-2 font-mono text-xs transition-all uppercase tracking-wider cursor-pointer ${
                isSelected
                  ? "bg-[#024d37] text-white font-bold shadow-xs"
                  : "text-muted-foreground hover:text-foreground hover:bg-muted"
              }`}
            >
              {srv.title.split(" ")[0]}
            </button>
          );
        })}
      </div>

      {/* 3-Tier Grid */}
      <div className="grid grid-cols-1 gap-8 lg:grid-cols-3 items-stretch">
        {/* 1. STARTER CARD */}
        <div className="group relative flex flex-col justify-between rounded-2xl border border-border bg-card p-8 overflow-hidden transition-colors duration-200 hover:border-[#024d37]/50">
          <BlinkingSquares
            squareSize={16}
            gap={6}
            color="#024d37"
            maxOpacity={0.25}
            minOpacity={0.02}
            blinkProbability={0.015}
            className="opacity-40"
          />
          <div className="relative z-10">
            <h3 className="text-xl font-bold tracking-tight text-foreground">
              {starterPkg.name}
            </h3>

            <p className="mt-2 text-xs text-muted-foreground leading-relaxed min-h-[36px]">
              {starterPkg.tagline}
            </p>

            <div className="mt-8 mb-6">
              <div className="flex items-baseline gap-1.5">
                <span className="text-4xl font-extrabold tracking-tight text-foreground font-mono">
                  {formatPrice(starterPkg.price_display)}
                </span>
                <span className="text-xs text-muted-foreground font-mono">/ milestone</span>
              </div>
              <span className="text-[10px] font-mono tracking-widest text-muted-foreground uppercase block mt-1">
                {t("pkg.fixed_scope")}
              </span>
            </div>

            <div className="space-y-3 pt-6 border-t border-border/80">
              {starterPkg.features.map((feat, i) => (
                <div key={i} className="flex items-start gap-3 text-xs text-foreground/90">
                  <Check className="h-4 w-4 text-[#024d37] shrink-0 mt-0.5" />
                  <span className="leading-snug">{feat}</span>
                </div>
              ))}
            </div>
          </div>

          <div className="mt-10 pt-4">
            <Link
              href={`/contact?package=${encodeURIComponent(starterPkg.name)}&service=${selectedServiceSlug}`}
              className="flex w-full items-center justify-center gap-2 rounded-full bg-foreground py-3.5 px-4 font-mono text-xs font-semibold uppercase tracking-[0.12em] text-background transition-opacity hover:opacity-90 active:scale-[0.98]"
            >
              <span>{t("pkg.start_with")} {starterPkg.name}</span>
              <ArrowRight className="h-3 w-3" />
            </Link>
          </div>
        </div>

        {/* 2. STUDIO CARD (MOST POPULAR - WITH BRAND GREEN #024d37 & BLINKING SQUARES) */}
        <div className="group relative flex flex-col justify-between rounded-2xl border-2 border-[#036549] bg-[#024d37] p-8 overflow-hidden transition-all duration-300 shadow-2xl shadow-[#024d37]/30 scale-[1.02]">
          {/* Animated Blinking Squares Canvas */}
          <BlinkingSquares
            squareSize={16}
            gap={6}
            color="#34d399"
            maxOpacity={0.4}
            minOpacity={0.04}
            blinkProbability={0.025}
            className="opacity-45"
          />

          {/* Subtle vignette overlay */}
          <div className="pointer-events-none absolute inset-0 bg-gradient-to-b from-[#024d37]/90 via-[#024d37]/75 to-[#024d37]/95" />

          <div className="relative z-10">
            <div className="flex items-center justify-between">
              <h3 className="text-2xl font-bold tracking-tight text-white font-sans">
                {popularPkg.name}
              </h3>
              <span className="inline-flex items-center gap-1 rounded-full bg-white/20 border border-white/40 px-3 py-1 font-mono text-[10px] font-bold uppercase tracking-wider text-white backdrop-blur-md shadow-sm">
                <Sparkles className="h-3 w-3 text-emerald-300" />
                {t("pkg.popular")}
              </span>
            </div>

            <p className="mt-2 text-xs text-white/80 leading-relaxed min-h-[36px]">
              {popularPkg.tagline}
            </p>

            <div className="mt-8 mb-6">
              <div className="flex items-baseline gap-1.5">
                <span className="text-4xl font-extrabold tracking-tight text-white font-mono">
                  {formatPrice(popularPkg.price_display)}
                </span>
                <span className="text-xs text-white/75 font-mono">/ milestone</span>
              </div>
              <span className="text-[10px] font-mono tracking-widest text-emerald-200 uppercase block mt-1">
                {t("pkg.billed_milestone")}
              </span>
            </div>

            <div className="space-y-3 pt-6 border-t border-white/20">
              {popularPkg.features.map((feat, i) => (
                <div key={i} className="flex items-start gap-3 text-xs text-white">
                  <div className="h-4 w-4 rounded-full bg-white/20 flex items-center justify-center shrink-0 mt-0.5">
                    <Check className="h-3 w-3 text-white" />
                  </div>
                  <span className="leading-snug">{feat}</span>
                </div>
              ))}
            </div>
          </div>

          <div className="relative z-10 mt-10 pt-4">
            <Link
              href={`/contact?package=${encodeURIComponent(popularPkg.name)}&service=${selectedServiceSlug}`}
              className="flex w-full items-center justify-center gap-2 rounded-full bg-white py-3.5 px-4 font-mono text-xs font-bold uppercase tracking-[0.12em] text-[#024d37] transition-all hover:bg-slate-100 hover:scale-[1.03] active:scale-[0.98] shadow-lg shadow-black/20"
            >
              <span>{t("pkg.start_with")} {popularPkg.name}</span>
              <ArrowRight className="h-3.5 w-3.5 text-[#024d37]" />
            </Link>
          </div>
        </div>

        {/* 3. ENTERPRISE CARD */}
        <div className="group relative flex flex-col justify-between rounded-2xl border border-border bg-card p-8 overflow-hidden transition-colors duration-200 hover:border-[#024d37]/50">
          <BlinkingSquares
            squareSize={16}
            gap={6}
            color="#024d37"
            maxOpacity={0.25}
            minOpacity={0.02}
            blinkProbability={0.015}
            className="opacity-40"
          />
          <div className="relative z-10">
            <h3 className="text-xl font-bold tracking-tight text-foreground">
              {enterprisePkg.name}
            </h3>

            <p className="mt-2 text-xs text-muted-foreground leading-relaxed min-h-[36px]">
              {enterprisePkg.tagline}
            </p>

            <div className="mt-8 mb-6">
              <div className="flex items-baseline gap-1.5">
                <span className="text-4xl font-extrabold tracking-tight text-foreground font-mono">
                  {formatPrice(enterprisePkg.price_display)}
                </span>
              </div>
              <span className="text-[10px] font-mono tracking-widest text-muted-foreground uppercase block mt-1">
                {t("pkg.volume_pricing")}
              </span>
            </div>

            <div className="space-y-3 pt-6 border-t border-border/80">
              {enterprisePkg.features.map((feat, i) => (
                <div key={i} className="flex items-start gap-3 text-xs text-foreground/90">
                  <Check className="h-4 w-4 text-[#024d37] shrink-0 mt-0.5" />
                  <span className="leading-snug">{feat}</span>
                </div>
              ))}
            </div>
          </div>

          <div className="mt-10 pt-4">
            <Link
              href={`/contact?package=${encodeURIComponent(enterprisePkg.name)}&service=${selectedServiceSlug}`}
              className="flex w-full items-center justify-center gap-2 rounded-full border border-border bg-muted py-3.5 px-4 font-mono text-xs font-semibold uppercase tracking-[0.12em] text-foreground transition-colors hover:bg-card hover:border-[#024d37]"
            >
              <span>{t("pkg.start_with")} {enterprisePkg.name}</span>
              <ArrowRight className="h-3 w-3" />
            </Link>
          </div>
        </div>
      </div>
    </div>
  );
}
