"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, Gauge, Layers, Ruler, FileCheck, Compass, Sliders } from "lucide-react";

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

export function ExtrusionsProfileTemplate({ product, corridors, corridor }: Props) {
  const [isRfqOpen, setIsRfqOpen] = useState(false);
  const [selectedProfile, setSelectedProfile] = useState("Co-Extruded Sponge + Solid EPDM with Steel Wire");

  const extrusionProfiles: Record<string, { durometer: string; compressionSet: string; ozoneResistance: string; recommendedUse: string }> = {
    "Co-Extruded Sponge + Solid EPDM with Steel Wire": {
      durometer: "Dual Durometer: 45 Shore A (Sponge Bulb) + 70 Shore A (Clamping Leg)",
      compressionSet: "< 20% (ASTM D1056 22h @ 70°C)",
      ozoneResistance: "No Cracks @ 1,000 pphm / 40°C (ASTM D1149)",
      recommendedUse: "Automotive door surrounds, electrical enclosure weatherproofing, switchgear cabinets",
    },
    "D-Section Hollow Sponge Rubber Gasket": {
      durometer: "40 Shore A (Microcellular EPDM Sponge, 0.45 g/cm³)",
      compressionSet: "< 18% High Elastic Recovery",
      ozoneResistance: "Excellent UV & Marine Weathering",
      recommendedUse: "Architectural curtain walls, structural aluminum glazing, acoustic HVAC dampening",
    },
    "P-Section Heavy Marine Hatch Seal": {
      durometer: "65 Shore A Solid EPDM / Neoprene",
      compressionSet: "< 22% (Severe Seawater Immersion)",
      ozoneResistance: "Tested to 1,500 hours Weatherometer",
      recommendedUse: "Container door gaskets, marine watertight hatch covers, railway gangway bellows",
    },
    "Food Grade Silicone Oven Door Seal": {
      durometer: "60 Shore A High-Tear Heat Stabilized Silicone",
      compressionSet: "< 15% @ 175°C",
      ozoneResistance: "Inert to Oxygen, Steam & UV",
      recommendedUse: "Commercial baking ovens, pharmaceutical autoclave doors, laboratory incubators",
    },
  };

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

  return (
    <div className="bg-[#0f172a] text-slate-100 min-h-screen selection:bg-orange-500 selection:text-black font-sans">
      {/* 1. Precision CAD Blueprint Hero */}
      <section className="relative overflow-hidden py-16 lg:py-24 border-b border-slate-700/60 bg-[radial-gradient(ellipse_at_top,_var(--tw-gradient-stops))] from-slate-800 via-[#0f172a] to-[#090d16]">
        {/* CAD Grid Background Overlay */}
        <div className="absolute inset-0 opacity-10 bg-[linear-gradient(to_right,#38bdf8_1px,transparent_1px),linear-gradient(to_bottom,#38bdf8_1px,transparent_1px)] bg-[size:32px_32px] pointer-events-none"></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-400 uppercase tracking-widest mb-3">
            <Compass className="w-4 h-4 text-sky-400" />
            <span>Continuous Extrusion &amp; Co-Molding Division • Microwave (UHF) Vulcanization</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 ? `• Exporting to ${corridor.countryName}` : ""}
              </h1>
              <p className="text-sm sm:text-base text-slate-300 max-w-2xl leading-relaxed">
                {product.shortDescription}
              </p>

              {/* Engineering Blueprint 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/50 text-sky-300 font-bold">
                  Tolerance: DIN ISO 3302-1 E2 / E1
                </span>
                <span className="px-3 py-1.5 rounded-lg bg-orange-950/70 border border-orange-500/50 text-orange-300 font-bold">
                  Curing: Continuous Microwave UHF Curing
                </span>
                <span className="px-3 py-1.5 rounded-lg bg-slate-800 border border-slate-700 text-slate-200 font-bold">
                  Weathering: ASTM D1149 Ozone Immunity
                </span>
              </div>

              <div className="pt-4 flex flex-wrap gap-4">
                <button
                  onClick={() => setIsRfqOpen(true)}
                  className="px-7 py-3.5 rounded-xl bg-orange-500 hover:bg-orange-400 text-black font-black text-sm shadow-xl shadow-orange-950/50 transition-all flex items-center gap-2"
                >
                  <Ruler className="w-4 h-4 text-black" />
                  Request Profile CAD Quotation
                </button>
                <Link
                  href="#profile-selector"
                  title="Navigate to Profile Selector"
                  className="px-6 py-3.5 rounded-xl border border-slate-700 hover:border-sky-400 bg-slate-900/80 text-slate-200 hover:text-white font-semibold text-sm transition-all"
                >
                  Cross-Section Durometer Tool &darr;
                </Link>
              </div>
            </div>

            <div className="lg:col-span-4">
              <div className="relative rounded-2xl bg-slate-900/90 border-2 border-sky-500/40 p-4 shadow-2xl backdrop-blur-xl">
                <div className="aspect-[4/3] rounded-xl overflow-hidden bg-black/60 flex items-center justify-center relative">
                  {/* 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-sky-500 text-black font-mono text-[10px] font-black">
                    HS: {product.hsCode}
                  </div>
                </div>

                <div className="mt-4 pt-3 border-t border-slate-800 grid grid-cols-2 gap-2 text-xs">
                  <div>
                    <span className="text-slate-500 block text-[10px] uppercase font-mono">Durometer</span>
                    <span className="font-bold text-sky-400">{product.hardnessShore}</span>
                  </div>
                  <div>
                    <span className="text-slate-500 block text-[10px] uppercase font-mono">Continuous Temp</span>
                    <span className="font-bold text-slate-200">{product.operatingTemp}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* 2. Interactive CAD Profile & Durometer Benchmarker */}
      <section id="profile-selector" className="py-16 bg-[#131d31] border-b border-slate-800">
        <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-sky-400 uppercase tracking-wider block mb-1">
              Extrusion Geometry & Compound Selector
            </span>
            <h2 className="text-2xl sm:text-3xl font-extrabold text-white">
              Continuous Co-Extrusion &amp; Sponge Density Tool
            </h2>
            <p className="text-sm text-slate-400 mt-2">
              Select cross-sectional geometry to examine compound elasticity, continuous compression recovery, and carrier reinforcement.
            </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-400 block font-semibold">
                Select Extrusion Profile Class:
              </label>
              {Object.keys(extrusionProfiles).map((profileKey) => (
                <button
                  key={profileKey}
                  onClick={() => setSelectedProfile(profileKey)}
                  className={`w-full text-left p-4 rounded-xl border transition-all flex items-center justify-between ${
                    selectedProfile === profileKey
                      ? "bg-sky-950/70 border-sky-400 text-white shadow-lg shadow-sky-950/40"
                      : "bg-slate-900/60 border-slate-800 text-slate-400 hover:border-slate-700 hover:text-slate-200"
                  }`}
                >
                  <span className="font-bold text-sm">{profileKey}</span>
                  <ChevronRight className={`w-4 h-4 ${selectedProfile === profileKey ? "text-sky-400" : "text-slate-600"}`} />
                </button>
              ))}
            </div>

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

                <h3 className="text-lg font-bold text-white mb-6 flex items-center gap-2">
                  <Sliders className="w-5 h-5 text-sky-400" />
                  Engineering Performance &amp; Material Analysis
                </h3>

                <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-6">
                  <div className="p-4 rounded-xl bg-slate-900 border border-slate-800">
                    <span className="text-[11px] font-mono uppercase text-slate-400 block">Durometer Specification</span>
                    <span className="text-sm font-mono font-bold text-sky-300">
                      {extrusionProfiles[selectedProfile].durometer}
                    </span>
                    <p className="text-[11px] text-slate-500 mt-1">Dual extrusion with wire-carrier retention</p>
                  </div>

                  <div className="p-4 rounded-xl bg-slate-900 border border-slate-800">
                    <span className="text-[11px] font-mono uppercase text-slate-400 block">Compression Deflection</span>
                    <span className="text-sm font-mono font-bold text-orange-400">
                      {extrusionProfiles[selectedProfile].compressionSet}
                    </span>
                    <p className="text-[11px] text-slate-500 mt-1">Low closing force for effortless door sealing</p>
                  </div>

                  <div className="p-4 rounded-xl bg-slate-900 border border-slate-800">
                    <span className="text-[11px] font-mono uppercase text-slate-400 block">Ozone & Weather Aging</span>
                    <span className="text-sm font-mono font-bold text-emerald-400">
                      {extrusionProfiles[selectedProfile].ozoneResistance}
                    </span>
                    <p className="text-[11px] text-slate-500 mt-1">Zero micro-cracking over 15-year outdoor exposure</p>
                  </div>

                  <div className="p-4 rounded-xl bg-slate-900 border border-slate-800">
                    <span className="text-[11px] font-mono uppercase text-slate-400 block">Recommended Application</span>
                    <span className="text-xs font-semibold text-slate-200">
                      {extrusionProfiles[selectedProfile].recommendedUse}
                    </span>
                  </div>
                </div>

                <div className="p-3.5 rounded-xl bg-sky-950/40 border border-sky-600/30 text-xs text-sky-200/90 flex items-start gap-2.5">
                  <ShieldCheck className="w-4 h-4 text-sky-400 shrink-0 mt-0.5" />
                  <span>
                    <strong>Secondary Fabrication Capabilities:</strong> In addition to continuous reel dispatch, Horiaki offers injection molded corner vulcanization, endless loop joining, and 3M acrylic foam pressure-sensitive adhesive tape pre-application.
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* 3. Extrusion Engineering Technology */}
      <section className="py-16 bg-[#0f172a] border-b border-slate-800">
        <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-sky-400 uppercase tracking-widest block mb-2">
              Advanced Tooling & Die Technology
            </span>
            <h2 className="text-2xl sm:text-4xl font-extrabold text-white">
              Continuous Microwave (UHF) Vulcanization Lines
            </h2>
            <p className="text-sm text-slate-400 mt-2">
              Ultra-smooth surface finishes, tight dimensional tolerances, and high line speed manufacturing for bulk global export.
            </p>
          </div>

          <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
            <div className="rounded-2xl bg-slate-900/60 border border-slate-800 p-6 space-y-3">
              <div className="w-10 h-10 rounded-xl bg-sky-950 border border-sky-500/40 flex items-center justify-center text-sky-400 font-bold">
                01
              </div>
              <h3 className="text-base font-bold text-white">Wire-EDM Tooling Dies</h3>
              <p className="text-xs text-slate-400 leading-relaxed">
                Custom stainless steel extrusion dies cut with multi-axis Wire EDM achieving ± 0.01 mm orifice accuracy for complex multi-cavity geometries.
              </p>
            </div>

            <div className="rounded-2xl bg-slate-900/60 border border-slate-800 p-6 space-y-3">
              <div className="w-10 h-10 rounded-xl bg-orange-950 border border-orange-500/40 flex items-center justify-center text-orange-400 font-bold">
                02
              </div>
              <h3 className="text-base font-bold text-white">Dual &amp; Tri-Extrusion</h3>
              <p className="text-xs text-slate-400 leading-relaxed">
                Co-extruding dense EPDM, microcellular sponge EPDM, and flexible steel wire or stamped steel carriers in a single continuous pass.
              </p>
            </div>

            <div className="rounded-2xl bg-slate-900/60 border border-slate-800 p-6 space-y-3">
              <div className="w-10 h-10 rounded-xl bg-emerald-950 border border-emerald-500/40 flex items-center justify-center text-emerald-400 font-bold">
                03
              </div>
              <h3 className="text-base font-bold text-white">Hot-Air &amp; Molten Salt Curing</h3>
              <p className="text-xs text-slate-400 leading-relaxed">
                High-temperature microwave tunnel followed by hot-air post-curing ensuring 100% cure index with zero porosity or surface blemishes.
              </p>
            </div>
          </div>
        </div>
      </section>

      {/* 4. Specifications Table */}
      <section className="py-16 bg-[#131d31] border-b border-slate-800">
        <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-white mb-6 flex items-center gap-2">
                <Layers className="w-5 h-5 text-sky-400" />
                Extrusion Profile Specifications
              </h2>

              <div className="overflow-hidden rounded-xl border border-slate-800">
                <table className="w-full text-xs text-left">
                  <thead className="bg-slate-900 text-slate-400 uppercase font-mono">
                    <tr>
                      <th className="p-3">Parameter</th>
                      <th className="p-3">Horiaki Manufacturing Standard</th>
                    </tr>
                  </thead>
                  <tbody className="divide-y divide-slate-800 text-slate-300 font-mono">
                    <tr>
                      <td className="p-3 text-slate-400">Elastomer Options</td>
                      <td className="p-3 font-semibold text-white">{product.materials?.join(", ") || "EPDM, Silicone, Nitrile, Neoprene, Fluoroelastomer"}</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-400">Hardness Shore Range</td>
                      <td className="p-3 font-semibold text-white">{product.hardnessShore}</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-400">Continuous Operating Temp</td>
                      <td className="p-3 font-semibold text-white">{product.operatingTemp}</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-400">Dimensional Tolerance</td>
                      <td className="p-3 font-semibold text-white">DIN ISO 3302-1 Class E1 (Precision) / E2 (Commercial)</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-400">Ozone Aging Resistance</td>
                      <td className="p-3 font-semibold text-white">No crack after 200h @ 50 pphm 40°C 20% elongation</td>
                    </tr>
                    <tr>
                      <td className="p-3 text-slate-400">Packaging Modes</td>
                      <td className="p-3 font-semibold text-sky-400">Continuous coils on wooden reels, pre-cut lengths, or vulcanized rings</td>
                    </tr>
                    {Object.entries(product.specs || {}).map(([key, val]) => (
                      <tr key={key}>
                        <td className="p-3 text-slate-400 capitalize">{key.replace(/([A-Z])/g, " $1")}</td>
                        <td className="p-3 font-semibold text-white">{String(val)}</td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            </div>

            <div className="lg:col-span-5 space-y-4">
              <h2 className="text-xl font-black text-white flex items-center gap-2">
                <Award className="w-5 h-5 text-sky-400" />
                Industry Application Arenas
              </h2>

              <div className="grid grid-cols-1 gap-2.5">
                {[
                  { title: "Architectural Glazing & Facades", desc: "Curtain wall pressure plate gaskets, thermal break EPDM profiles, structural silicone seals." },
                  { title: "Transportation & Railway Vehicles", desc: "Door edge safety bumpers, window glazing rubber channels, gangway bellows." },
                  { title: "Electrical Switchgear & Cabinets", desc: "IP65/IP66 enclosure door seals with self-gripping spring steel carrier." },
                  { title: "Shipping Containers & Reefer Vans", desc: "Multi-lip container door gaskets resisting extreme sub-zero ocean humidity." },
                ].map((item, idx) => (
                  <div key={idx} className="p-3.5 rounded-xl bg-slate-900 border border-slate-800">
                    <h3 className="text-sm font-bold text-sky-300">{item.title}</h3>
                    <p className="text-xs text-slate-400 mt-1">{item.desc}</p>
                  </div>
                ))}
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* 5. Destination Corridor Logistics */}
      {corridor && (
        <section className="py-16 bg-[#0a0f1d] border-b border-slate-800">
          <div className="max-w-7xl mx-auto px-4">
            <div className="p-8 rounded-2xl bg-gradient-to-br from-slate-900 to-black border border-sky-950/60">
              <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-sky-400 uppercase">
                    Extrusion Export Corridor: India to {corridor.countryName}
                  </span>
                  <h3 className="text-2xl font-black text-white mt-1">
                    Ocean Container Stuffing to {corridor.seaports.map((p) => p.name).join(", ")}
                  </h3>
                </div>
                <div className="text-right">
                  <span className="text-xs text-slate-500 font-mono block">Ocean Transit from Chennai Port</span>
                  <span className="text-xl font-bold text-sky-400 font-mono">
                    {corridor.seaports[0]?.transitDays || "18-24 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-950/80 border border-slate-800">
                  <span className="text-slate-500 block text-[10px] uppercase">Compliance Framework</span>
                  <span className="font-bold text-slate-200">{corridor.complianceStandards.join(", ")}</span>
                </div>
                <div className="p-4 rounded-xl bg-slate-950/80 border border-slate-800">
                  <span className="text-slate-500 block text-[10px] uppercase">Automotive / Facade Hubs</span>
                  <span className="font-bold text-slate-200">{corridor.industrialClusters.slice(0, 3).join(", ")}</span>
                </div>
                <div className="p-4 rounded-xl bg-slate-950/80 border border-slate-800">
                  <span className="text-slate-500 block text-[10px] uppercase">Packaging Standard</span>
                  <span className="font-bold text-orange-300">Continuous Reels / Heavy Duty Corrugated FCL</span>
                </div>
              </div>
            </div>
          </div>
        </section>
      )}

      {/* 6. Lead Generation Footer */}
      <section className="py-16 bg-gradient-to-b from-[#131d31] to-black">
        <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 Rubber Extrusions &amp; Co-Molded Profiles
          </h2>
          <p className="text-sm text-slate-400 max-w-xl mx-auto">
            Send us your 2D/3D CAD profile drawing (.DWG, .DXF, .STEP). Horiaki India Private Limited develops precision extrusion dies in 7-10 days with initial sample reel dispatch.
          </p>
          <div className="flex flex-wrap justify-center gap-4 pt-2">
            <button
              onClick={() => setIsRfqOpen(true)}
              className="px-8 py-4 rounded-xl bg-orange-500 hover:bg-orange-400 text-black font-black text-sm shadow-xl shadow-orange-950 transition-all flex items-center gap-2"
            >
              <Ruler className="w-4 h-4 text-black" />
              Upload CAD Drawing for Tooling RFQ
            </button>
            <a
              href="https://wa.me/919840023478?text=Hello%20Horiaki%20Extrusions%20Team%2C%20I%20have%20a%20drawing%20for%20custom%20rubber%20profiles"
              target="_blank"
              rel="noopener noreferrer"
              title="Direct WhatsApp CAD Consultation with Horiaki Extrusions Team"
              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"
            >
              Direct WhatsApp CAD Consultation
            </a>
          </div>
        </div>
      </section>

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