"use client";

import React, { useState } from "react";
import Link from "next/link";
import { ProductData, CorridorDataParsed } from "@/lib/types";
import { RFQModal } from "../RFQModal";
import { ShieldCheck, Award, CheckCircle2, ChevronRight, Activity, Thermometer, Sparkles, Layers, FileCheck, Stethoscope } from "lucide-react";

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

export function MedicalEvThermalTemplate({ product, corridors, corridor }: Props) {
  const [isRfqOpen, setIsRfqOpen] = useState(false);
  const [selectedConductivity, setSelectedConductivity] = useState("3.0 W/m·K (EV Battery Module Gap Filler)");

  const thermalGrades: Record<string, { thermalImpedance: string; breakdownVoltage: string; flameRating: string; biocompatibility: string }> = {
    "1.5 W/m·K (USP Class VI Medical Silicone)": {
      thermalImpedance: "0.85 °C·cm²/W @ 50 psi",
      breakdownVoltage: "> 12 kV/mm (ASTM D149)",
      flameRating: "UL 94 HB Self-Extinguishing",
      biocompatibility: "USP Class VI & FDA 21 CFR 177.2600 Certified (Platinum Cured)",
    },
    "3.0 W/m·K (EV Battery Module Gap Filler)": {
      thermalImpedance: "0.45 °C·cm²/W @ 50 psi",
      breakdownVoltage: "> 10 kV/mm (ASTM D149)",
      flameRating: "UL 94 V-0 Rated (Zero Flame Drip)",
      biocompatibility: "Halogen Free, RoHS 3 & REACH SVHC Compliant",
    },
    "5.0 W/m·K (Power Inverter Thermal Interface)": {
      thermalImpedance: "0.28 °C·cm²/W @ 50 psi",
      breakdownVoltage: "> 8 kV/mm (ASTM D149)",
      flameRating: "UL 94 V-0 Certified",
      biocompatibility: "Low Outgassing: CVCM < 0.05% (NASA SP-R-0022A)",
    },
    "8.0 W/m·K (Ultra-Thermal Aerospace Interface)": {
      thermalImpedance: "0.18 °C·cm²/W @ 50 psi",
      breakdownVoltage: "> 6 kV/mm (ASTM D149)",
      flameRating: "UL 94 V-0 Certified",
      biocompatibility: "ASTM E595 Space Grade Outgassing Compliance",
    },
  };

  const imageSrc = product.image ? (product.image.startsWith("http") ? product.image : `/uploaded_files/${product.image}`) : "/uploaded_files/8d7b66bc144a5f.jpg";

  return (
    <div className="bg-[#f8fafc] text-slate-800 min-h-screen selection:bg-teal-500 selection:text-white">
      {/* 1. Clinical Cleanroom & High-Precision Hero */}
      <section className="relative overflow-hidden py-16 lg:py-24 border-b border-teal-100 bg-gradient-to-br from-white via-teal-50/50 to-cyan-50/40">
        <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-teal-700 uppercase tracking-widest mb-3">
            <Stethoscope className="w-4 h-4 text-teal-600" />
            <span>Medical Grade Platinum Silicone & EV Thermal Interface Division • Cleanroom Packaged</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-slate-900 leading-tight font-sans">
                {product.name} {corridor ? `• Export to ${corridor.countryName}` : ""}
              </h1>
              <p className="text-sm sm:text-base text-slate-600 max-w-2xl leading-relaxed">
                {product.shortDescription}
              </p>

              {/* Sterile & EV Specs 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-teal-100/80 border border-teal-300 text-teal-900 font-bold">
                  Biocompatibility: USP Class VI & ISO 10993
                </span>
                <span className="px-3 py-1.5 rounded-lg bg-cyan-100/80 border border-cyan-300 text-cyan-900 font-bold">
                  Operating Temp: {product.operatingTemp}
                </span>
                <span className="px-3 py-1.5 rounded-lg bg-emerald-100/80 border border-emerald-300 text-emerald-900 font-bold">
                  Flame Rating: UL 94 V-0 Certified
                </span>
              </div>

              <div className="pt-4 flex flex-wrap gap-4">
                <button
                  onClick={() => setIsRfqOpen(true)}
                  className="px-7 py-3.5 rounded-xl bg-teal-600 hover:bg-teal-700 text-white font-bold text-sm shadow-xl shadow-teal-900/10 transition-all flex items-center gap-2"
                >
                  <Sparkles className="w-4 h-4" />
                  Request Medical/EV Quote
                </button>
                <Link
                  href="#thermal-simulator"
                  title="Navigate to Thermal Stability Simulator"
                  className="px-6 py-3.5 rounded-xl border border-teal-200 hover:border-teal-400 bg-white text-slate-700 hover:text-teal-900 font-semibold text-sm transition-all shadow-sm"
                >
                  Thermal &amp; Dielectric Simulator &darr;
                </Link>
              </div>
            </div>

            <div className="lg:col-span-4">
              <div className="relative rounded-2xl bg-white border border-teal-100 p-4 shadow-xl">
                <div className="aspect-[4/3] rounded-xl overflow-hidden bg-slate-50 flex items-center justify-center relative border border-slate-100">
                  {/* eslint-disable-next-line @next/next/no-img-element */}
                  <img
                    src={imageSrc}
                    alt={product.name}
                    className="w-full h-full object-contain p-4 transition-transform duration-500 hover:scale-105"
                  />
                  <div className="absolute top-3 left-3 px-2 py-1 rounded bg-teal-600 text-white font-mono text-[10px] font-bold">
                    HS: {product.hsCode}
                  </div>
                </div>

                <div className="mt-4 pt-3 border-t border-slate-100 grid grid-cols-2 gap-2 text-xs">
                  <div>
                    <span className="text-slate-400 block text-[10px] uppercase font-mono">Durometer</span>
                    <span className="font-bold text-teal-800">{product.hardnessShore}</span>
                  </div>
                  <div>
                    <span className="text-slate-400 block text-[10px] uppercase font-mono">Curing System</span>
                    <span className="font-bold text-slate-700">Platinum Addition Cured</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* 2. Interactive Thermal & Dielectric Simulator */}
      <section id="thermal-simulator" className="py-16 bg-white border-b border-slate-200">
        <div className="max-w-7xl mx-auto px-4">
          <div className="max-w-3xl mb-8">
            <span className="text-xs font-mono font-bold text-teal-600 uppercase tracking-wider block mb-1">
              Thermal Management & Electrical Insulation Tool
            </span>
            <h2 className="text-2xl sm:text-3xl font-extrabold text-slate-900">
              Thermal Conductivity (W/m·K) &amp; Dielectric Breakdown Simulator
            </h2>
            <p className="text-sm text-slate-500 mt-2">
              Select target thermal dissipation conductivity to evaluate contact impedance, breakdown voltage, and outgassing characteristics.
            </p>
          </div>

          <div className="grid grid-cols-1 lg:grid-cols-12 gap-8 items-start">
            <div className="lg:col-span-5 space-y-3">
              <label className="text-xs font-mono uppercase text-slate-500 block font-semibold">
                Thermal Interface Grade:
              </label>
              {Object.keys(thermalGrades).map((gradeKey) => (
                <button
                  key={gradeKey}
                  onClick={() => setSelectedConductivity(gradeKey)}
                  className={`w-full text-left p-4 rounded-xl border transition-all flex items-center justify-between ${
                    selectedConductivity === gradeKey
                      ? "bg-teal-50 border-teal-500 text-teal-950 shadow-md shadow-teal-100"
                      : "bg-slate-50/80 border-slate-200 text-slate-600 hover:border-slate-300 hover:text-slate-900"
                  }`}
                >
                  <span className="font-bold text-sm">{gradeKey}</span>
                  <ChevronRight className={`w-4 h-4 ${selectedConductivity === gradeKey ? "text-teal-600" : "text-slate-400"}`} />
                </button>
              ))}
            </div>

            <div className="lg:col-span-7">
              <div className="rounded-2xl bg-slate-900 text-white p-6 shadow-xl relative overflow-hidden">
                <div className="absolute top-0 right-0 px-4 py-1.5 bg-teal-500/20 border-b border-l border-teal-400/40 text-teal-300 font-mono text-[11px] font-bold rounded-bl-xl">
                  Active Simulation: {selectedConductivity.split(" ")[0]}
                </div>

                <h3 className="text-lg font-bold text-white mb-6 flex items-center gap-2">
                  <Thermometer className="w-5 h-5 text-teal-400" />
                  Thermal Dissipation &amp; Electrical Performance
                </h3>

                <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-6">
                  <div className="p-4 rounded-xl bg-slate-800/80 border border-slate-700">
                    <span className="text-[11px] font-mono uppercase text-slate-400 block">Thermal Contact Impedance</span>
                    <span className="text-lg font-mono font-black text-teal-400">
                      {thermalGrades[selectedConductivity].thermalImpedance}
                    </span>
                    <p className="text-[11px] text-slate-400 mt-1">Conforms under low assembly pressure</p>
                  </div>

                  <div className="p-4 rounded-xl bg-slate-800/80 border border-slate-700">
                    <span className="text-[11px] font-mono uppercase text-slate-400 block">Dielectric Breakdown Voltage</span>
                    <span className="text-lg font-mono font-black text-emerald-400">
                      {thermalGrades[selectedConductivity].breakdownVoltage}
                    </span>
                    <p className="text-[11px] text-slate-400 mt-1">Prevents high-voltage battery short circuits</p>
                  </div>

                  <div className="p-4 rounded-xl bg-slate-800/80 border border-slate-700">
                    <span className="text-[11px] font-mono uppercase text-slate-400 block">Flammability Safety</span>
                    <span className="text-lg font-mono font-black text-emerald-400">
                      {thermalGrades[selectedConductivity].flameRating}
                    </span>
                    <p className="text-[11px] text-slate-400 mt-1">Meets automotive battery fire enclosure standards</p>
                  </div>

                  <div className="p-4 rounded-xl bg-slate-800/80 border border-slate-700">
                    <span className="text-[11px] font-mono uppercase text-slate-400 block">Compliance & Outgassing</span>
                    <span className="text-xs font-semibold text-slate-200">
                      {thermalGrades[selectedConductivity].biocompatibility}
                    </span>
                  </div>
                </div>

                <div className="p-3.5 rounded-xl bg-teal-950/60 border border-teal-500/30 text-xs text-teal-200/90 flex items-start gap-2.5">
                  <ShieldCheck className="w-4 h-4 text-teal-400 shrink-0 mt-0.5" />
                  <span>
                    <strong>Platinum Addition Cure Advantage:</strong> 100% peroxide-free vulcanization eliminates volatile organic acid blooming and post-cure odor, certified for direct pharmaceutical contact and high-vacuum electronics.
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* 3. Clinical & Food Contact Purity Certifications */}
      <section className="py-16 bg-slate-50 border-b border-slate-200">
        <div className="max-w-7xl mx-auto px-4">
          <div className="text-center max-w-3xl mx-auto mb-12">
            <span className="text-xs font-mono font-bold text-teal-600 uppercase tracking-widest block mb-2">
              Verified Compliance Standards
            </span>
            <h2 className="text-2xl sm:text-4xl font-extrabold text-slate-900">
              Medical Biocompatibility &amp; Food Contact Approvals
            </h2>
            <p className="text-sm text-slate-500 mt-2">
              Independently tested by accredited laboratories for complete safety in critical medical devices and food processing pipelines.
            </p>
          </div>

          <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
            <div className="rounded-2xl bg-white border border-slate-200 p-6 space-y-3 shadow-sm">
              <div className="w-10 h-10 rounded-xl bg-teal-50 border border-teal-200 flex items-center justify-center text-teal-700 font-bold">
                01
              </div>
              <h3 className="text-base font-bold text-slate-900">USP Class VI &amp; ISO 10993</h3>
              <p className="text-xs text-slate-500 leading-relaxed">
                Tested for systemic toxicity, intracutaneous reactivity, and muscle implantation. Safe for short-term and continuous physiological contact.
              </p>
            </div>

            <div className="rounded-2xl bg-white border border-slate-200 p-6 space-y-3 shadow-sm">
              <div className="w-10 h-10 rounded-xl bg-cyan-50 border border-cyan-200 flex items-center justify-center text-cyan-700 font-bold">
                02
              </div>
              <h3 className="text-base font-bold text-slate-900">FDA 21 CFR 177.2600</h3>
              <p className="text-xs text-slate-500 leading-relaxed">
                Extractables compliant with US FDA regulations for repeated food contact in beverage dispensing, dairy processing, and confectionery molding.
              </p>
            </div>

            <div className="rounded-2xl bg-white border border-slate-200 p-6 space-y-3 shadow-sm">
              <div className="w-10 h-10 rounded-xl bg-emerald-50 border border-emerald-200 flex items-center justify-center text-emerald-700 font-bold">
                03
              </div>
              <h3 className="text-base font-bold text-slate-900">NASA Low Outgassing (ASTM E595)</h3>
              <p className="text-xs text-slate-500 leading-relaxed">
                CVCM &lt; 0.1% and TML &lt; 1.0% certified for aerospace thermal optics and optical sensor enclosures, avoiding volatile lens condensation.
              </p>
            </div>
          </div>
        </div>
      </section>

      {/* 4. Specifications Table */}
      <section className="py-16 bg-white border-b border-slate-200">
        <div className="max-w-7xl mx-auto px-4">
          <div className="grid grid-cols-1 lg:grid-cols-12 gap-8 items-start">
            <div className="lg:col-span-7">
              <h2 className="text-2xl font-black text-slate-900 mb-6 flex items-center gap-2">
                <Layers className="w-5 h-5 text-teal-600" />
                Physical & Thermal Specifications
              </h2>

              <div className="overflow-hidden rounded-xl border border-slate-200">
                <table className="w-full text-xs text-left">
                  <thead className="bg-slate-100 text-slate-600 uppercase font-mono">
                    <tr>
                      <th className="p-3">Specification Parameter</th>
                      <th className="p-3">Certified Performance Value</th>
                    </tr>
                  </thead>
                  <tbody className="divide-y divide-slate-200 text-slate-700 font-mono">
                    <tr>
                      <td className="p-3 text-slate-500">Polymer Base</td>
                      <td className="p-3 font-semibold text-slate-900">100% Pure Liquid Silicone Rubber (LSR) / HTV Silicone</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-500">Hardness Shore Range</td>
                      <td className="p-3 font-semibold text-slate-900">{product.hardnessShore}</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-500">Continuous Operating Temp</td>
                      <td className="p-3 font-semibold text-slate-900">{product.operatingTemp}</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-500">Tensile Strength (ASTM D412)</td>
                      <td className="p-3 font-semibold text-slate-900">&gt; 9.5 MPa</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-500">Tear Strength (ASTM D624 Die B)</td>
                      <td className="p-3 font-semibold text-slate-900">&gt; 35 kN/m</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-500">Certifications</td>
                      <td className="p-3 font-semibold text-teal-700">{product.certifications?.join(", ") || "USP Class VI, FDA 21 CFR 177.2600, UL 94 V-0"}</td>
                    </tr>
                    {Object.entries(product.specs || {}).map(([key, val]) => (
                      <tr key={key}>
                        <td className="p-3 text-slate-500 capitalize">{key.replace(/([A-Z])/g, " $1")}</td>
                        <td className="p-3 font-semibold text-slate-900">{String(val)}</td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            </div>

            <div className="lg:col-span-5 space-y-4">
              <h2 className="text-xl font-black text-slate-900 flex items-center gap-2">
                <Award className="w-5 h-5 text-teal-600" />
                Target Industries &amp; Critical Use Cases
              </h2>

              <div className="grid grid-cols-1 gap-2.5">
                {[
                  { title: "Electric Vehicle (EV) Battery Packs", desc: "Thermal gap filler pads between pouch cells, cold plate cushioning, flame barrier gaskets." },
                  { title: "Medical Devices & Diagnostics", desc: "Peristaltic pump tubing, ventilator diaphragms, fluid manifold silicone gaskets." },
                  { title: "Aerospace & Avionics Enclosures", desc: "Hermetic high-altitude silicone seals, low-outgassing optical enclosure gaskets." },
                  { title: "Hygienic Food & Beverage Lines", desc: "Tri-clamp sanitary gaskets, brewery valve seals, high-temperature dairy tubing." },
                ].map((item, idx) => (
                  <div key={idx} className="p-3.5 rounded-xl bg-slate-50 border border-slate-200">
                    <h3 className="text-sm font-bold text-teal-800">{item.title}</h3>
                    <p className="text-xs text-slate-600 mt-1">{item.desc}</p>
                  </div>
                ))}
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* 5. Destination Corridor */}
      {corridor && (
        <section className="py-16 bg-slate-100 border-b border-slate-200">
          <div className="max-w-7xl mx-auto px-4">
            <div className="p-8 rounded-2xl bg-white border border-slate-200 shadow-sm">
              <div className="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-6">
                <div>
                  <span className="text-xs font-mono font-bold text-teal-700 uppercase">
                    Export Corridor: India to {corridor.countryName}
                  </span>
                  <h3 className="text-2xl font-black text-slate-900 mt-1">
                    Air &amp; Ocean Logistics to {corridor.seaports.map((p) => p.name).join(", ")}
                  </h3>
                </div>
                <div className="text-right">
                  <span className="text-xs text-slate-500 font-mono block">Express Air Courier (DHL / FedEx)</span>
                  <span className="text-xl font-bold text-teal-700 font-mono">
                    3-5 Business Days
                  </span>
                </div>
              </div>

              <div className="grid grid-cols-1 md:grid-cols-3 gap-4 text-xs font-mono">
                <div className="p-4 rounded-xl bg-slate-50 border border-slate-200">
                  <span className="text-slate-500 block text-[10px] uppercase">Compliance Framework</span>
                  <span className="font-bold text-slate-800">{corridor.complianceStandards.join(", ")}</span>
                </div>
                <div className="p-4 rounded-xl bg-slate-50 border border-slate-200">
                  <span className="text-slate-500 block text-[10px] uppercase">Medical & EV Clusters</span>
                  <span className="font-bold text-slate-800">{corridor.industrialClusters.slice(0, 3).join(", ")}</span>
                </div>
                <div className="p-4 rounded-xl bg-slate-50 border border-slate-200">
                  <span className="text-slate-500 block text-[10px] uppercase">Packaging & QC</span>
                  <span className="font-bold text-teal-800">Cleanroom Polybagging + COA / MTR</span>
                </div>
              </div>
            </div>
          </div>
        </section>
      )}

      {/* 6. Lead Generation Footer */}
      <section className="py-16 bg-slate-900 text-white">
        <div className="max-w-4xl mx-auto px-4 text-center space-y-6">
          <h2 className="text-3xl sm:text-4xl font-black text-white">
            Custom Medical Silicone &amp; EV Thermal Interface Manufacturing
          </h2>
          <p className="text-sm text-slate-300 max-w-xl mx-auto">
            Horiaki India Private Limited produces to stringent cleanroom specifications with full lot traceability, USP Class VI test certificates, and custom die-cut thermal sheets.
          </p>
          <div className="flex flex-wrap justify-center gap-4 pt-2">
            <button
              onClick={() => setIsRfqOpen(true)}
              className="px-8 py-4 rounded-xl bg-teal-500 hover:bg-teal-400 text-slate-950 font-black text-sm shadow-xl shadow-teal-950 transition-all flex items-center gap-2"
            >
              <Sparkles className="w-4 h-4" />
              Request Silicone / Thermal RFQ
            </button>
            <a
              href="https://wa.me/919840023478?text=Hello%20Horiaki%20Medical%20Silicone%20Division%2C%20I%20need%20a%20quotation%20for%20silicone%20components"
              target="_blank"
              rel="noopener noreferrer"
              title="WhatsApp Technical Consultation with Horiaki Medical Silicone Division"
              className="px-8 py-4 rounded-xl bg-emerald-600 hover:bg-emerald-500 text-white font-bold text-sm shadow-xl shadow-emerald-950 transition-all"
            >
              WhatsApp Technical Consultation
            </a>
          </div>
        </div>
      </section>

      <RFQModal
        isOpen={isRfqOpen}
        onClose={() => setIsRfqOpen(false)}
        productName={product.name}
        countryName={corridor?.countryName}
      />
    </div>
  );
}
