import type { Metadata } from 'next';
import Link from 'next/link';
import { notFound } from 'next/navigation';
import Bot from 'lucide-react/dist/esm/icons/bot';
import Truck from 'lucide-react/dist/esm/icons/truck';
import Sparkles from 'lucide-react/dist/esm/icons/sparkles';
import CircleUserRound from 'lucide-react/dist/esm/icons/circle-user-round';
import Cog from 'lucide-react/dist/esm/icons/cog';
import Compass from 'lucide-react/dist/esm/icons/compass';
import Cpu from 'lucide-react/dist/esm/icons/cpu';
import ScanLine from 'lucide-react/dist/esm/icons/scan-line';
import Search from 'lucide-react/dist/esm/icons/search';
import Layers from 'lucide-react/dist/esm/icons/layers';
import Bell from 'lucide-react/dist/esm/icons/bell';
import UtensilsCrossed from 'lucide-react/dist/esm/icons/utensils-crossed';
import ChevronRight from 'lucide-react/dist/esm/icons/chevron-right';
import ArrowRight from 'lucide-react/dist/esm/icons/arrow-right';
import { Navbar } from '@/components/Navbar';
import { FooterAndContact } from '@/components/FooterAndContact';
import { MotionSection } from '@/components/ui/MotionSection';
import { CTAButton } from '@/components/ui/CTAButton';
import { INDUSTRIES, getIndustryBySlug } from '@/data/industries';
import { INDUSTRY_PAGES, type IndustrySolution } from '@/data/industry-pages';

type Params = { slug: string };

const ICONS: Record<IndustrySolution['iconKey'], React.ComponentType<{ size?: number; strokeWidth?: number }>> = {
  service: Bell,
  delivery: Truck,
  cleaning: Sparkles,
  humanoid: CircleUserRound,
  quadruped: Bot,
  sensor: ScanLine,
  inspect: Search,
  platform: Cpu,
  guide: Compass,
  queue: Layers,
  host: CircleUserRound,
  tray: UtensilsCrossed,
};

export function generateStaticParams() {
  return INDUSTRIES.map((i) => ({ slug: i.slug }));
}

export async function generateMetadata({ params }: { params: Promise<Params> }): Promise<Metadata> {
  const { slug } = await params;
  const industry = getIndustryBySlug(slug);
  if (!industry) return {};
  const content = INDUSTRY_PAGES[industry.id];
  if (!content) return {};
  const url = `/industries/${industry.slug}/`;
  return {
    title: content.seoTitle,
    description: content.seoDescription,
    alternates: { canonical: url },
    openGraph: {
      title: content.seoTitle,
      description: content.seoDescription,
      url,
      type: 'website',
    },
    twitter: {
      card: 'summary_large_image',
      title: content.seoTitle,
      description: content.seoDescription,
    },
  };
}

export default async function IndustryDetailPage({ params }: { params: Promise<Params> }) {
  const { slug } = await params;
  const industry = getIndustryBySlug(slug);
  if (!industry) notFound();
  const content = INDUSTRY_PAGES[industry.id];
  if (!content) notFound();

  const accent = industry.accent;

  return (
    <>
      <Navbar />

      <main style={{ paddingTop: 'clamp(5rem, 9vw, 7rem)', paddingBottom: 'clamp(2rem, 5vw, 4rem)' }}>
        <div className="container-wide" style={{ display: 'flex', flexDirection: 'column', gap: 'clamp(2.5rem, 5vw, 4rem)' }}>
          {/* BREADCRUMBS */}
          <nav aria-label="Breadcrumb" className="ip-breadcrumbs">
            <ol>
              <li><Link href="/">Home</Link></li>
              <li aria-hidden><ChevronRight size={12} /></li>
              <li><Link href="/industries/">Industries</Link></li>
              <li aria-hidden><ChevronRight size={12} /></li>
              <li aria-current="page">{industry.name}</li>
            </ol>
          </nav>

          {/* HERO */}
          <MotionSection>
            <section className="ip-hero" style={{ ['--acc' as string]: accent }}>
              <div className="ip-hero-glow" aria-hidden />
              <div className="ip-hero-grid" aria-hidden />
              <div className="ip-hero-inner">
                <div className="ip-hero-copy">
                  <span className="eyebrow">{industry.name}</span>
                  <h1 className="ip-hero-title">
                    <span className="text-gradient">{content.heroTitle}</span>
                  </h1>
                  <p className="ip-hero-sub">{content.heroSubtitle}</p>
                  <div className="ip-hero-actions">
                    <CTAButton href="/book-demo/" variant="primary" size="lg" arrow="up-right">
                      Book a Demo
                    </CTAButton>
                    <CTAButton href="/contact/" variant="secondary" size="lg" arrow="right">
                      Talk to Our Team
                    </CTAButton>
                  </div>
                </div>
                <aside className="ip-hero-card" aria-hidden>
                  <div className="ip-hero-card-tag">YSL · UAE</div>
                  <div className="ip-hero-card-icon">
                    <Bot size={40} strokeWidth={1.4} />
                  </div>
                  <div className="ip-hero-card-meta">
                    <span>Industry</span>
                    <strong>{industry.name}</strong>
                  </div>
                </aside>
              </div>
            </section>
          </MotionSection>

          {/* PROBLEM */}
          <MotionSection>
            <section className="ip-block">
              <header className="ip-block-head">
                <span className="eyebrow">The challenge</span>
                <h2 className="ip-block-title">Where today’s operations break down.</h2>
              </header>
              <ul className="ip-problems" role="list">
                {content.problemPoints.map((p, i) => (
                  <li key={i} className="ip-problem">
                    <span className="ip-problem-num">{String(i + 1).padStart(2, '0')}</span>
                    <p>{p}</p>
                  </li>
                ))}
              </ul>
            </section>
          </MotionSection>

          {/* SOLUTIONS */}
          <MotionSection>
            <section className="ip-block" style={{ ['--acc' as string]: accent }}>
              <header className="ip-block-head">
                <span className="eyebrow">Recommended robot solutions</span>
                <h2 className="ip-block-title">The right robot families for {industry.name.toLowerCase()}.</h2>
              </header>
              <div className="ip-solutions">
                {content.solutions.map((s, i) => {
                  const Icon = ICONS[s.iconKey] ?? Cog;
                  return (
                    <article key={i} className="ip-solution">
                      <span className="ip-solution-icon" aria-hidden>
                        <Icon size={20} strokeWidth={1.6} />
                      </span>
                      <h3>{s.title}</h3>
                      <p>{s.body}</p>
                    </article>
                  );
                })}
              </div>
            </section>
          </MotionSection>

          {/* USE CASES */}
          <MotionSection>
            <section className="ip-block">
              <header className="ip-block-head">
                <span className="eyebrow">Use cases</span>
                <h2 className="ip-block-title">Where these robots earn their keep.</h2>
              </header>
              <ul className="ip-usecases" role="list">
                {content.useCases.map((u, i) => (
                  <li key={i} className="ip-usecase">
                    <span className="ip-usecase-bullet" aria-hidden />
                    <span>{u}</span>
                  </li>
                ))}
              </ul>
            </section>
          </MotionSection>

          {/* BENEFITS */}
          <MotionSection>
            <section className="ip-block">
              <header className="ip-block-head">
                <span className="eyebrow">Business benefits</span>
                <h2 className="ip-block-title">What this changes for your operation.</h2>
              </header>
              <div className="ip-benefits">
                {content.benefits.map((b, i) => (
                  <div key={i} className="ip-benefit">
                    <h3>{b.title}</h3>
                    <p>{b.body}</p>
                  </div>
                ))}
              </div>
            </section>
          </MotionSection>

          {/* SUGGESTED ROBOTS */}
          <MotionSection>
            <section className="ip-block" style={{ ['--acc' as string]: accent }}>
              <header className="ip-block-head">
                <span className="eyebrow">Suggested robots</span>
                <h2 className="ip-block-title">Brands and families we deploy for this sector.</h2>
              </header>
              <div className="ip-robots">
                {content.suggestedRobots.map((r, i) => (
                  <article key={i} className="ip-robot">
                    <span className="ip-robot-brand">{r.brand}</span>
                    <h3>{r.family}</h3>
                    <p>{r.body}</p>
                    <Link href="/robots/" className="ip-robot-link">
                      Browse robots <ArrowRight size={13} strokeWidth={2} />
                    </Link>
                  </article>
                ))}
              </div>
            </section>
          </MotionSection>

          {/* CTA */}
          <MotionSection>
            <section className="ip-cta">
              <div className="ip-cta-glow" aria-hidden />
              <div className="ip-cta-inner">
                <div className="ip-cta-text">
                  <span className="eyebrow">Next step</span>
                  <h2 className="ip-cta-title">Find the right robot for your venue.</h2>
                  <p>
                    Tell us about your space, workflow, and goals. Our Dubai team will recommend the best-fit robotics solution and arrange a live demo or quotation.
                  </p>
                </div>
                <div className="ip-cta-actions">
                  <CTAButton href="/book-demo/" variant="primary" size="lg" arrow="up-right">
                    Book a Live Demo
                  </CTAButton>
                  <CTAButton href="/contact/" variant="secondary" size="lg" arrow="right">
                    Request UAE Quotation
                  </CTAButton>
                  <CTAButton
                    href="https://wa.me/971500000000"
                    external
                    variant="ghost"
                    size="lg"
                    arrow="up-right"
                    ariaLabel="Chat with YS Lootah Robotics on WhatsApp"
                  >
                    WhatsApp Us
                  </CTAButton>
                </div>
              </div>
            </section>
          </MotionSection>
        </div>
      </main>

      <FooterAndContact />

      <style>{`
        .ip-breadcrumbs ol {
          list-style: none;
          margin: 0;
          padding: 0;
          display: flex;
          flex-wrap: wrap;
          align-items: center;
          gap: 0.5rem;
          font-size: 0.72rem;
          letter-spacing: 0.18em;
          text-transform: uppercase;
          color: #8891C7;
        }
        .ip-breadcrumbs a {
          color: #8891C7;
          text-decoration: none;
          transition: color 0.25s;
        }
        .ip-breadcrumbs a:hover { color: #FFFFFF; }
        .ip-breadcrumbs li[aria-current="page"] { color: #FFFFFF; }
        .ip-breadcrumbs li[aria-hidden] { display: inline-flex; align-items: center; color: #4a4f63; }

        /* HERO */
        .ip-hero {
          position: relative;
          overflow: hidden;
          border-radius: 24px;
          border: 1px solid rgba(74, 102, 216, 0.18);
          background:
            radial-gradient(ellipse 70% 100% at 0% 0%, rgba(58, 85, 196, 0.18), transparent 55%),
            linear-gradient(135deg, rgba(14, 13, 22, 0.92), rgba(6, 6, 10, 0.97));
          box-shadow: 0 30px 90px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.04);
        }
        .ip-hero-glow {
          position: absolute;
          width: 520px;
          height: 520px;
          border-radius: 999px;
          top: -180px;
          right: -160px;
          background: radial-gradient(circle, color-mix(in srgb, var(--acc) 35%, transparent), transparent 70%);
          filter: blur(110px);
          opacity: 0.6;
          pointer-events: none;
        }
        .ip-hero-grid {
          position: absolute;
          inset: 0;
          background:
            linear-gradient(rgba(74, 102, 216, 0.05) 1px, transparent 1px),
            linear-gradient(90deg, rgba(74, 102, 216, 0.05) 1px, transparent 1px);
          background-size: 56px 56px;
          mask-image: radial-gradient(ellipse 70% 90% at 30% 40%, #000 25%, transparent 80%);
          -webkit-mask-image: radial-gradient(ellipse 70% 90% at 30% 40%, #000 25%, transparent 80%);
          pointer-events: none;
        }
        .ip-hero-inner {
          position: relative;
          z-index: 1;
          display: grid;
          grid-template-columns: minmax(0, 1fr);
          gap: clamp(1.5rem, 3vw, 2.5rem);
          padding: clamp(1.8rem, 4vw, 3.2rem);
          align-items: center;
        }
        .ip-hero-copy { display: flex; flex-direction: column; gap: 1rem; max-width: 640px; }
        .ip-hero-title {
          margin: 0;
          font-size: clamp(2rem, 5vw, 3.2rem);
          line-height: 1.05;
          font-weight: 300;
          letter-spacing: -0.03em;
          color: #FFFFFF;
        }
        .ip-hero-title .text-gradient { font-weight: 500; }
        .ip-hero-sub {
          margin: 0;
          color: #DEE0F0;
          font-size: clamp(0.95rem, 1.8vw, 1.1rem);
          line-height: 1.65;
        }
        .ip-hero-actions { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-top: 0.35rem; }

        .ip-hero-card {
          display: none;
          position: relative;
          padding: 1.1rem 1.2rem;
          border-radius: 18px;
          border: 1px solid rgba(74, 102, 216, 0.22);
          background:
            radial-gradient(ellipse 80% 80% at 50% 0%, color-mix(in srgb, var(--acc) 18%, transparent), transparent 60%),
            linear-gradient(180deg, rgba(20, 19, 26, 0.85), rgba(8, 8, 12, 0.95));
        }
        .ip-hero-card-tag {
          font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
          font-size: 0.62rem;
          letter-spacing: 0.32em;
          font-weight: 800;
          color: #DEE0F0;
        }
        .ip-hero-card-icon {
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 1.2rem 0;
          color: color-mix(in srgb, var(--acc) 75%, white);
        }
        .ip-hero-card-meta {
          display: flex;
          flex-direction: column;
          gap: 0.2rem;
          padding-top: 0.6rem;
          border-top: 1px solid rgba(222, 224, 240, 0.08);
        }
        .ip-hero-card-meta span {
          font-size: 0.6rem;
          letter-spacing: 0.28em;
          text-transform: uppercase;
          color: #8891C7;
          font-weight: 700;
        }
        .ip-hero-card-meta strong { color: #FFFFFF; font-size: 0.95rem; font-weight: 600; }

        @media (min-width: 920px) {
          .ip-hero-inner { grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); }
          .ip-hero-card { display: block; }
        }

        /* BLOCKS */
        .ip-block { display: flex; flex-direction: column; gap: clamp(1.1rem, 2vw, 1.5rem); }
        .ip-block-head { display: flex; flex-direction: column; gap: 0.5rem; max-width: 720px; }
        .ip-block-title {
          margin: 0;
          font-size: clamp(1.5rem, 3vw, 2.1rem);
          font-weight: 400;
          letter-spacing: -0.02em;
          color: #FFFFFF;
          line-height: 1.15;
        }

        /* PROBLEMS */
        .ip-problems {
          list-style: none;
          margin: 0;
          padding: 0;
          display: grid;
          grid-template-columns: minmax(0, 1fr);
          gap: 0.75rem;
        }
        @media (min-width: 720px) {
          .ip-problems { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
        }
        .ip-problem {
          display: grid;
          grid-template-columns: auto minmax(0, 1fr);
          gap: 0.85rem;
          padding: 1rem 1.1rem;
          border-radius: 16px;
          border: 1px solid rgba(222, 224, 240, 0.08);
          background: linear-gradient(135deg, rgba(20, 19, 26, 0.72), rgba(10, 10, 14, 0.92));
        }
        .ip-problem-num {
          font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
          font-size: 0.68rem;
          letter-spacing: 0.22em;
          font-weight: 800;
          color: #8891C7;
          padding-top: 0.15rem;
        }
        .ip-problem p {
          margin: 0;
          color: #DEE0F0;
          font-size: 0.92rem;
          line-height: 1.55;
        }

        /* SOLUTIONS */
        .ip-solutions {
          display: grid;
          grid-template-columns: minmax(0, 1fr);
          gap: 1rem;
        }
        @media (min-width: 640px) { .ip-solutions { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
        @media (min-width: 1100px) { .ip-solutions { grid-template-columns: repeat(4, minmax(0, 1fr)); } }

        .ip-solution {
          display: flex;
          flex-direction: column;
          gap: 0.55rem;
          padding: 1.1rem 1.15rem 1.15rem;
          border-radius: 16px;
          border: 1px solid rgba(222, 224, 240, 0.10);
          background:
            radial-gradient(ellipse 80% 60% at 100% 0%, color-mix(in srgb, var(--acc) 14%, transparent), transparent 60%),
            linear-gradient(180deg, rgba(22, 21, 30, 0.88), rgba(10, 10, 14, 0.95));
          transition: border-color 0.3s, transform 0.3s, box-shadow 0.3s;
        }
        .ip-solution:hover {
          transform: translateY(-2px);
          border-color: rgba(74, 102, 216, 0.4);
          box-shadow: 0 16px 36px rgba(0, 0, 0, 0.5), 0 0 22px rgba(58, 85, 196, 0.18);
        }
        .ip-solution-icon {
          display: inline-flex;
          align-items: center;
          justify-content: center;
          width: 38px;
          height: 38px;
          border-radius: 11px;
          color: color-mix(in srgb, var(--acc) 80%, white);
          border: 1px solid color-mix(in srgb, var(--acc) 30%, transparent);
          background: color-mix(in srgb, var(--acc) 12%, rgba(14, 13, 18, 0.55));
        }
        .ip-solution h3 {
          margin: 0;
          font-size: 1rem;
          font-weight: 600;
          color: #FFFFFF;
          letter-spacing: -0.005em;
        }
        .ip-solution p {
          margin: 0;
          color: #C9CCDE;
          font-size: 0.85rem;
          line-height: 1.55;
        }

        /* USE CASES */
        .ip-usecases {
          list-style: none;
          margin: 0;
          padding: 0;
          display: grid;
          grid-template-columns: minmax(0, 1fr);
          gap: 0.6rem;
        }
        @media (min-width: 720px) {
          .ip-usecases { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.8rem; }
        }
        .ip-usecase {
          display: grid;
          grid-template-columns: 10px minmax(0, 1fr);
          align-items: center;
          gap: 0.8rem;
          padding: 0.85rem 1rem;
          border-radius: 14px;
          border: 1px solid rgba(222, 224, 240, 0.08);
          background: linear-gradient(135deg, rgba(18, 18, 26, 0.7), rgba(8, 8, 12, 0.88));
          color: #DEE0F0;
          font-size: 0.92rem;
        }
        .ip-usecase-bullet {
          width: 8px;
          height: 8px;
          border-radius: 999px;
          background: #4a66d8;
          box-shadow: 0 0 12px rgba(74, 102, 216, 0.7);
        }

        /* BENEFITS */
        .ip-benefits {
          display: grid;
          grid-template-columns: minmax(0, 1fr);
          gap: 0.85rem;
        }
        @media (min-width: 640px) { .ip-benefits { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
        @media (min-width: 1100px) { .ip-benefits { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
        .ip-benefit {
          padding: 1.1rem 1.15rem;
          border-radius: 16px;
          border: 1px solid rgba(222, 224, 240, 0.08);
          background: linear-gradient(135deg, rgba(20, 19, 26, 0.78), rgba(10, 10, 14, 0.92));
        }
        .ip-benefit h3 {
          margin: 0 0 0.4rem;
          font-size: 0.95rem;
          font-weight: 600;
          color: #FFFFFF;
        }
        .ip-benefit p {
          margin: 0;
          color: #C9CCDE;
          font-size: 0.85rem;
          line-height: 1.5;
        }

        /* SUGGESTED ROBOTS */
        .ip-robots {
          display: grid;
          grid-template-columns: minmax(0, 1fr);
          gap: 0.95rem;
        }
        @media (min-width: 720px) { .ip-robots { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
        @media (min-width: 1100px) { .ip-robots { grid-template-columns: repeat(3, minmax(0, 1fr)); } }

        .ip-robot {
          display: flex;
          flex-direction: column;
          gap: 0.5rem;
          padding: 1.15rem 1.2rem 1.1rem;
          border-radius: 16px;
          border: 1px solid rgba(74, 102, 216, 0.22);
          background:
            radial-gradient(ellipse 80% 60% at 100% 0%, color-mix(in srgb, var(--acc) 18%, transparent), transparent 60%),
            linear-gradient(180deg, rgba(22, 21, 30, 0.9), rgba(10, 10, 14, 0.95));
        }
        .ip-robot-brand {
          font-size: 0.6rem;
          letter-spacing: 0.3em;
          text-transform: uppercase;
          font-weight: 800;
          color: color-mix(in srgb, var(--acc) 70%, white);
        }
        .ip-robot h3 {
          margin: 0;
          font-size: 1.05rem;
          font-weight: 600;
          color: #FFFFFF;
        }
        .ip-robot p {
          margin: 0;
          color: #C9CCDE;
          font-size: 0.88rem;
          line-height: 1.55;
          flex: 1;
        }
        .ip-robot-link {
          display: inline-flex;
          align-items: center;
          gap: 0.45rem;
          margin-top: 0.5rem;
          font-size: 0.8rem;
          font-weight: 600;
          color: #DEE0F0;
          text-decoration: none;
          transition: color 0.25s, gap 0.25s;
        }
        .ip-robot-link:hover { color: #FFFFFF; gap: 0.7rem; }

        /* CTA */
        .ip-cta {
          position: relative;
          overflow: hidden;
          border-radius: 24px;
          border: 1px solid rgba(74, 102, 216, 0.22);
          background:
            radial-gradient(ellipse 60% 100% at 100% 0%, rgba(58, 85, 196, 0.18), transparent 60%),
            linear-gradient(135deg, rgba(18, 16, 28, 0.92), rgba(8, 8, 12, 0.96));
          box-shadow: 0 30px 90px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.04);
        }
        .ip-cta-glow {
          position: absolute;
          width: 480px;
          height: 480px;
          border-radius: 999px;
          top: -160px;
          left: -160px;
          background: radial-gradient(circle, rgba(74, 102, 216, 0.4), transparent 70%);
          filter: blur(100px);
          opacity: 0.55;
          pointer-events: none;
        }
        .ip-cta-inner {
          position: relative;
          z-index: 1;
          display: grid;
          grid-template-columns: minmax(0, 1fr);
          gap: clamp(1.2rem, 3vw, 2rem);
          padding: clamp(1.6rem, 3.5vw, 2.6rem);
          align-items: center;
        }
        @media (min-width: 900px) {
          .ip-cta-inner { grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); }
        }
        .ip-cta-text { display: flex; flex-direction: column; gap: 0.6rem; max-width: 560px; }
        .ip-cta-title {
          margin: 0;
          font-size: clamp(1.6rem, 3vw, 2.2rem);
          font-weight: 400;
          letter-spacing: -0.02em;
          color: #FFFFFF;
          line-height: 1.1;
        }
        .ip-cta-text p { margin: 0; color: #DEE0F0; font-size: 0.95rem; line-height: 1.6; }
        .ip-cta-actions {
          display: flex;
          flex-wrap: wrap;
          gap: 0.55rem;
          justify-content: flex-start;
        }
        @media (min-width: 900px) {
          .ip-cta-actions { justify-content: flex-end; }
        }
        @media (max-width: 520px) {
          .ip-cta-actions .cta-btn { width: 100%; justify-content: space-between; }
          .ip-hero-actions .cta-btn { width: 100%; justify-content: space-between; }
        }
      `}</style>
    </>
  );
}
