"use client";

import React, { useState } from "react";
import Link from "next/link";
import { ProductData, CorridorDataParsed } from "@/lib/types";
import { RFQModal } from "../RFQModal";
import { Droplet, ShieldCheck, Award, Anchor, CheckCircle2, ChevronRight, Gauge, Wrench, FileCheck } from "lucide-react";

interface Props {
  product: ProductData;
  corridors: CorridorDataParsed[];
  corridor?: CorridorDataParsed;
}

export function WaterGasPipelineTemplate({ product, corridors, corridor }: Props) {
  const [isRfqOpen, setIsRfqOpen] = useState(false);
  const [selectedDn, setSelectedDn] = useState("DN 100 (4 Inch)");

  const flangeTorques: Record<string, { pn16Torque: string; pn40Torque: string; boltSize: string; blowOutSafety: string }> = {
    "DN 50 (2 Inch)": { pn16Torque: "45 Nm", pn40Torque: "60 Nm", boltSize: "4x M16", blowOutSafety: "> 100 Bar" },
    "DN 80 (3 Inch)": { pn16Torque: "65 Nm", pn40Torque: "90 Nm", boltSize: "8x M16", blowOutSafety: "> 100 Bar" },
    "DN 100 (4 Inch)": { pn16Torque: "85 Nm", pn40Torque: "120 Nm", boltSize: "8x M16", blowOutSafety: "> 100 Bar" },
    "DN 150 (6 Inch)": { pn16Torque: "140 Nm", pn40Torque: "195 Nm", boltSize: "8x M20", blowOutSafety: "> 90 Bar" },
    "DN 200 (8 Inch)": { pn16Torque: "180 Nm", pn40Torque: "260 Nm", boltSize: "12x M20", blowOutSafety: "> 90 Bar" },
    "DN 300 (12 Inch)": { pn16Torque: "240 Nm", pn40Torque: "380 Nm", boltSize: "12x M24", blowOutSafety: "> 80 Bar" },
  };

  return (
    <div className="bg-slate-100 text-slate-900 min-h-screen">
      {/* 1. Industrial Blueprint & Municipal Waterworks Hero */}
      <section className="relative overflow-hidden py-16 lg:py-24 border-b border-blue-900/20 bg-gradient-to-br from-[#0c2340] via-[#10355f] to-[#0a1a2e] text-white">
        <div className="absolute inset-0 opacity-15 bg-[linear-gradient(to_right,#38bdf8_1px,transparent_1px),linear-gradient(to_bottom,#38bdf8_1px,transparent_1px)] bg-[size:24px_24px]"></div>

        <div className="max-w-7xl mx-auto px-4 relative z-10">
          <div className="flex items-center gap-2 text-xs font-mono font-bold text-sky-300 uppercase tracking-widest mb-3">
            <Droplet className="w-4 h-4 text-sky-400" />
            <span>Municipal Waterworks, Gas Infrastructure & Flange Sealing Engineering</span>
          </div>

          <div className="grid grid-cols-1 lg:grid-cols-12 gap-8 items-center">
            <div className="lg:col-span-8 space-y-4">
              <h1 className="text-3xl sm:text-5xl font-black tracking-tight text-white leading-tight font-sans">
                {product.name} {corridor ? `• Export to ${corridor.countryName}` : ""}
              </h1>
              <p className="text-sm sm:text-base text-slate-200 max-w-2xl leading-relaxed">
                {product.shortDescription}
              </p>

              {/* Engineering Metric Badges */}
              <div className="flex flex-wrap gap-2.5 pt-2 font-mono text-xs">
                <span className="px-3 py-1.5 rounded-lg bg-sky-950/80 border border-sky-400/40 text-sky-200 font-bold">
                  Pressure: {product.pressureRating}
                </span>
                <span className="px-3 py-1.5 rounded-lg bg-blue-950/80 border border-blue-400/40 text-blue-200 font-bold">
                  Standard: EN 1514-1 / ASME B16.21
                </span>
                <span className="px-3 py-1.5 rounded-lg bg-emerald-950/80 border border-emerald-400/40 text-emerald-200 font-bold">
                  Drinking Water: UBA KTW-BWGL & DVGW W270
                </span>
              </div>

              <div className="pt-4 flex flex-wrap gap-4">
                <button
                  onClick={() => setIsRfqOpen(true)}
                  className="px-6 py-3 bg-gradient-to-r from-sky-400 to-blue-600 hover:from-sky-300 hover:to-blue-500 text-slate-950 font-black text-xs uppercase tracking-wider rounded-xl shadow-lg transition transform active:scale-95"
                >
                  Request Flange Pack & Bulk Price
                </button>
                <a
                  href={`https://wa.me/919840023478?text=Hello%20Horiaki%20Pipeline%20Desk%2C%20we%20require%20technical%20drawing%20for%20${encodeURIComponent(product.name)}.`}
                  target="_blank"
                  rel="noopener noreferrer"
                  title={`Request technical drawing for ${product.name} on WhatsApp`}
                  className="px-6 py-3 rounded-xl border border-sky-400/50 hover:bg-white/10 text-white font-bold text-xs uppercase tracking-wider transition"
                >
                  WhatsApp Pipeline Engineer
                </a>
              </div>
            </div>

            {/* Official German Certification Stamp Card */}
            <div className="lg:col-span-4 bg-white/10 border border-sky-300/30 rounded-2xl p-6 shadow-2xl backdrop-blur-md space-y-4">
              <div className="flex items-center justify-between border-b border-sky-300/30 pb-3">
                <div className="flex items-center gap-2">
                  <Award className="w-5 h-5 text-amber-300" />
                  <span className="text-xs font-mono font-bold text-white">GERMAN UBA CONFORMITY</span>
                </div>
                <span className="text-[10px] font-mono px-2 py-0.5 rounded bg-emerald-500/30 text-emerald-300 font-bold">
                  PASSED
                </span>
              </div>
              <div className="space-y-2 text-xs font-mono text-slate-200">
                <div className="flex justify-between">
                  <span className="text-slate-300">Drinking Water Approval:</span>
                  <span className="text-emerald-300 font-bold">UBA KTW-BWGL</span>
                </div>
                <div className="flex justify-between">
                  <span className="text-slate-300">Microbial Harmlessness:</span>
                  <span className="text-sky-300 font-bold">DVGW W270 Certified</span>
                </div>
                <div className="flex justify-between">
                  <span className="text-slate-300">Water Pipeline Standard:</span>
                  <span className="text-white font-bold">DIN EN 681-1</span>
                </div>
                <div className="flex justify-between">
                  <span className="text-slate-300">Gas Line Tightness:</span>
                  <span className="text-white font-bold">DIN EN 682 / DIN EN 549</span>
                </div>
              </div>
              <div className="pt-2 border-t border-sky-300/30 text-[11px] text-sky-200 flex items-center gap-1.5">
                <FileCheck className="w-4 h-4 text-emerald-400" />
                <span>EN 10204 3.1 Inspection Certificate Ready</span>
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* 2. Interactive Flange Bolt Torque & Tightening Calculator */}
      <section className="max-w-7xl mx-auto px-4 py-12">
        <div className="bg-white border border-slate-300/80 rounded-2xl p-6 lg:p-8 shadow-md space-y-6">
          <div className="flex flex-col md:flex-row md:items-center justify-between gap-4 border-b border-slate-200 pb-4">
            <div>
              <div className="inline-flex items-center gap-1.5 text-xs font-mono text-blue-700 uppercase tracking-wider mb-1 font-bold">
                <Wrench className="w-4 h-4 text-blue-600" />
                <span>Pipeline Contractor Engineering Tool</span>
              </div>
              <h2 className="text-xl sm:text-2xl font-black text-slate-900">
                Flange Bolt Tightening Torque & Blowout Safety Guide
              </h2>
            </div>
            <div className="text-xs text-slate-500 font-mono">
              Calculated per DIN EN 1514-1 & ASME B16.5 Flanges
            </div>
          </div>

          <div className="grid grid-cols-1 md:grid-cols-12 gap-6 items-center">
            <div className="md:col-span-5 space-y-2">
              <label className="block text-xs font-mono text-slate-700 font-bold">
                Select Nominal Pipe Diameter (DN):
              </label>
              <div className="grid grid-cols-2 gap-2">
                {Object.keys(flangeTorques).map((dn) => (
                  <button
                    key={dn}
                    onClick={() => setSelectedDn(dn)}
                    className={`px-3 py-2.5 rounded-xl text-xs font-mono font-bold transition text-left flex items-center justify-between ${
                      selectedDn === dn
                        ? "bg-[#0c2340] text-white shadow-md"
                        : "bg-slate-100 text-slate-700 hover:bg-slate-200"
                    }`}
                  >
                    <span>{dn}</span>
                  </button>
                ))}
              </div>
            </div>

            <div className="md:col-span-7 bg-slate-50 border border-slate-200 rounded-xl p-6 space-y-4 font-mono">
              <div className="flex items-center justify-between">
                <span className="text-xs text-slate-500 uppercase font-semibold">Nominal Size:</span>
                <span className="text-sm font-bold text-blue-900">{selectedDn}</span>
              </div>
              <div className="grid grid-cols-2 sm:grid-cols-3 gap-3 text-xs">
                <div className="p-3 bg-white rounded-lg border border-slate-200">
                  <span className="text-slate-400 block text-[11px]">PN 16 Torque:</span>
                  <span className="text-blue-900 font-bold text-sm">
                    {flangeTorques[selectedDn]?.pn16Torque}
                  </span>
                </div>
                <div className="p-3 bg-white rounded-lg border border-slate-200">
                  <span className="text-slate-400 block text-[11px]">PN 40 Torque:</span>
                  <span className="text-indigo-900 font-bold text-sm">
                    {flangeTorques[selectedDn]?.pn40Torque}
                  </span>
                </div>
                <div className="p-3 bg-white rounded-lg border border-slate-200">
                  <span className="text-slate-400 block text-[11px]">Blow-Out Surge:</span>
                  <span className="text-emerald-700 font-bold text-sm">
                    {flangeTorques[selectedDn]?.blowOutSafety}
                  </span>
                </div>
              </div>
              <p className="text-xs text-slate-600 leading-relaxed font-sans pt-2">
                Horiaki vulcanized rubber-steel gaskets eliminate cold-flow creeping, enabling reliable seals at up to
                <strong> 50% lower bolt tightening torque</strong> than standard compressed fiber or spiral wound gaskets.
              </p>
            </div>
          </div>
        </div>
      </section>

      {/* 3. Specifications & Corridors */}
      <section className="max-w-7xl mx-auto px-4 pb-16">
        <div className="grid grid-cols-1 lg:grid-cols-12 gap-8">
          <div className="lg:col-span-8 space-y-6">
            <div className="bg-white border border-slate-200 rounded-2xl p-6 shadow-sm space-y-4">
              <h3 className="text-lg font-bold text-slate-900">Technical Pipeline Specifications</h3>
              <div className="divide-y divide-slate-100 font-mono text-xs">
                <div className="py-2.5 flex justify-between">
                  <span className="text-slate-500">Operating Temperature:</span>
                  <span className="text-slate-900 font-bold">{product.operatingTemp}</span>
                </div>
                <div className="py-2.5 flex justify-between">
                  <span className="text-slate-500">Pressure Rating:</span>
                  <span className="text-blue-900 font-bold">{product.pressureRating}</span>
                </div>
                <div className="py-2.5 flex justify-between">
                  <span className="text-slate-500">Elastomer Hardness:</span>
                  <span className="text-slate-900">{product.hardnessShore}</span>
                </div>
                <div className="py-2.5 flex justify-between">
                  <span className="text-slate-500">Standard Export HS Code:</span>
                  <span className="text-slate-900">{product.hsCode}</span>
                </div>
                {Object.entries(product.specs).map(([key, val], idx) => (
                  <div key={idx} className="py-2.5 flex justify-between">
                    <span className="text-slate-500">{key}:</span>
                    <span className="text-slate-900">{val}</span>
                  </div>
                ))}
              </div>
            </div>

            <div className="bg-white border border-slate-200 rounded-2xl p-6 shadow-sm space-y-3">
              <h3 className="text-lg font-bold text-slate-900">Engineering Advantages</h3>
              <div className="space-y-2 text-xs text-slate-700">
                {product.features.map((feat, idx) => (
                  <div key={idx} className="flex items-start gap-2.5 p-3 rounded-xl bg-slate-50 border border-slate-200/60">
                    <CheckCircle2 className="w-4 h-4 text-emerald-600 shrink-0 mt-0.5" />
                    <span className="font-medium">{feat}</span>
                  </div>
                ))}
              </div>
            </div>
          </div>

          {/* Sidebar */}
          <div className="lg:col-span-4 space-y-6">
            <div className="bg-gradient-to-br from-[#0c2340] to-slate-900 rounded-2xl p-6 text-white space-y-4 shadow-xl">
              <span className="text-xs font-mono uppercase text-sky-300 font-bold block">Water & Gas Export Desk</span>
              <h4 className="text-xl font-bold">FCL Container Stuffing Direct to European Ports</h4>
              <p className="text-xs text-slate-300 leading-relaxed font-sans">
                Supplying municipal water boards in Germany (Hamburg), the Netherlands (Rotterdam), and Spain.
                Packed on ISPM 15 heat-treated Euro wooden pallets with shrink-wrap.
              </p>
              <button
                onClick={() => setIsRfqOpen(true)}
                className="w-full py-3 bg-sky-400 hover:bg-sky-300 text-slate-950 font-black text-xs uppercase tracking-wider rounded-xl transition"
              >
                Request FOB / CIF Quotation
              </button>
            </div>

            <div className="bg-white border border-slate-200 rounded-2xl p-6 text-xs space-y-3 font-mono shadow-sm">
              <h4 className="font-bold text-slate-900 text-sm border-b border-slate-100 pb-2">
                Municipal Water Corridors
              </h4>
              <div className="space-y-1.5">
                {corridors.slice(0, 6).map((c) => (
                  <Link
                    key={c.id}
                    href={`/${c.slug}/${product.slug}`}
                    title={`View ${product.name} export corridor for ${c.countryName}`}
                    className="flex items-center justify-between p-2 rounded-lg hover:bg-slate-50 text-slate-700 hover:text-blue-900 transition"
                  >
                    <span>{c.flag} {c.countryName}</span>
                    <span className="text-blue-700 font-bold">{c.seaports[0]?.transitDays}</span>
                  </Link>
                ))}
              </div>
            </div>
          </div>
        </div>
      </section>

      <RFQModal
        isOpen={isRfqOpen}
        onClose={() => setIsRfqOpen(false)}
        defaultProduct={product.name}
        defaultCountry={corridor?.countryName || "International"}
      />
    </div>
  );
}
