import { notFound } from "next/navigation";

import {
  buildIndustryMetadata,
  IndustryPageTemplate,
} from "@/components/marketing/IndustryPageTemplate";
import { industryPages } from "@/lib/industry-pages";

type Cta = {
  href: string;
  label: string;
};

const toolsMap: Record<string, { href: string; title: string; description: string }> = {
  "url-qr-code": { href: "/tools/url-qr-code", title: "URL QR Code Generator", description: "Create a scannable link to any webpage and update the destination any time." },
  "wifi-qr-code": { href: "/tools/wifi-qr-code", title: "WiFi QR Code Generator", description: "Print once, let guests scan to connect, and remove the friction of sharing passwords." },
  "vcard-qr-code": { href: "/tools/vcard-qr-code", title: "vCard QR Code", description: "Share contact details with a single scan so prospects can save your team instantly." },
  "email-qr-code": { href: "/tools/email-qr-code", title: "Email QR Code", description: "Open a pre-addressed email draft so customers can contact you faster." },
  "sms-qr-code": { href: "/tools/sms-qr-code", title: "SMS QR Code", description: "Pre-fill a text message to support bookings, follow-ups, and reminder flows." },
  "instagram-qr-code": { href: "/tools/instagram-qr-code", title: "Instagram QR Code", description: "Send visitors directly to your Instagram profile, gallery, or creator page." },
  "facebook-qr-code": { href: "/tools/facebook-qr-code", title: "Facebook QR Code", description: "Link to your Facebook Page or Group with a single scan." },
  "youtube-qr-code": { href: "/tools/youtube-qr-code", title: "YouTube QR Code", description: "Point visitors to a video, product demo, tutorial, or playlist." },
  "video-qr-code": { href: "/tools/youtube-qr-code", title: "Video QR Code", description: "Use a video-focused QR workflow for demos, walkthroughs, and visual proof." },
  "event-qr-code": { href: "/tools/event-qr-code", title: "Event QR Code", description: "Let visitors save event details directly to their phone calendar." },
  "app-store-qr-code": { href: "/tools/url-qr-code", title: "App Download QR Code", description: "Use a URL QR code to send scans to your App Store or Play Store listing." },
  "geolocation-qr-code": { href: "/tools/geolocation-qr-code", title: "Location QR Code", description: "Open Google Maps navigation to your store, venue, or office address." },
  "call-qr-code-generator": { href: "/tools/call-qr-code-generator", title: "Phone Call QR Code", description: "Open the dialer with your number pre-filled for instant contact." },
  "paypal-qr-code": { href: "/tools/paypal-qr-code", title: "PayPal QR Code", description: "Link directly to a PayPal payment, invoice, or donation page." },
  "pdf-qr-code": { href: "/tools/url-qr-code", title: "PDF QR Code", description: "Use a URL QR code to open a hosted PDF like a menu, brochure, or operating guide." },
  "whatsapp-qr-code": { href: "/tools/whatsapp-qr-code", title: "WhatsApp QR Code", description: "Open a WhatsApp chat with your number pre-loaded for faster support." },
};

const industryPrimaryCtas: Record<string, Cta> = {
  restaurants: { href: "/dynamic-qr-code-generator", label: "Create a Dynamic Menu QR Code" },
  cafes: { href: "/tools/url-qr-code", label: "Create a Cafe QR Code" },
  hotels: { href: "/tools/wifi-qr-code", label: "Create a Hotel WiFi QR Code" },
  "real-estate": { href: "/qr-code-tracking", label: "Track Real Estate QR Scans" },
  retail: { href: "/bulk-qr-code-generator", label: "Create Bulk Retail QR Codes" },
  events: { href: "/tools/event-qr-code", label: "Create an Event QR Code" },
  "doctors-dentists": { href: "/tools/url-qr-code", label: "Create an Intake Form QR Code" },
  dentists: { href: "/tools/url-qr-code", label: "Create a Dental QR Code" },
  "trade-shows": { href: "/qr-code-tracking", label: "Track Trade Show QR Leads" },
  pharmacies: { href: "/tools/email-qr-code", label: "Create a Pharmacy Follow-Up QR Code" },
  "car-dealerships": { href: "/qr-code-tracking", label: "Track Dealership QR Scans" },
  "pet-grooming": { href: "/tools/url-qr-code", label: "Create a Grooming Booking QR Code" },
  veterinarians: { href: "/tools/url-qr-code", label: "Create a Veterinary QR Code" },
};

const industrySecondaryCtas: Record<string, Cta> = {
  restaurants: { href: "/qr-code-tracking", label: "See QR Tracking Features" },
  cafes: { href: "/dynamic-qr-code-generator", label: "Try Dynamic QR Codes" },
  hotels: { href: "/dynamic-qr-code-generator", label: "Try Dynamic QR Codes" },
  "real-estate": { href: "/dynamic-qr-code-generator", label: "Try Dynamic QR Codes" },
  retail: { href: "/qr-code-tracking", label: "See QR Tracking Features" },
  events: { href: "/qr-code-tracking", label: "See QR Tracking Features" },
  "doctors-dentists": { href: "/qr-code-tracking", label: "See QR Tracking Features" },
  dentists: { href: "/qr-code-tracking", label: "See QR Tracking Features" },
  "trade-shows": { href: "/bulk-qr-code-generator", label: "Create Bulk QR Codes" },
};

export function generateStaticParams() {
  return industryPages.map((page) => ({ industry: page.slug }));
}

export function generateMetadata({ params }: { params: { industry: string } }) {
  const page = industryPages.find((entry) => entry.slug === params.industry);
  if (!page) return {};

  return buildIndustryMetadata({
    title: page.metaTitle || page.title,
    fallbackTitle: page.title,
    description: page.metaDescription,
    canonicalPath: `/qr-code-for/${page.slug}`,
  });
}

function getToolCta(toolSlug?: string): Cta | null {
  if (!toolSlug) return null;
  const tool = toolsMap[toolSlug];
  if (!tool) return null;

  if (toolSlug === "url-qr-code") {
    return { href: tool.href, label: "Create a QR Code" };
  }

  return { href: tool.href, label: `Create a ${tool.title}` };
}

function getPrimaryCta(page: (typeof industryPages)[number]): Cta {
  return (
    industryPrimaryCtas[page.slug] ??
    getToolCta(page.tools[0]) ?? {
      href: "/dynamic-qr-code-generator",
      label: "Create a Dynamic QR Code",
    }
  );
}

function getSecondaryCta(page: (typeof industryPages)[number]): Cta {
  return (
    industrySecondaryCtas[page.slug] ?? {
      href: "/pricing",
      label: "View Plans",
    }
  );
}

function getHeroImageAlt(page: (typeof industryPages)[number]) {
  return `${page.name} QR code example showing a real-world workflow for ${page.useCases[0]?.toLowerCase() ?? "customer engagement"}.`;
}

export default function IndustryPage({ params }: { params: { industry: string } }) {
  const page = industryPages.find((entry) => entry.slug === params.industry);
  if (!page) notFound();

  const primaryCta = getPrimaryCta(page);
  const secondaryCta = getSecondaryCta(page);
  const supportLinks = page.tools.map(
    (slug) =>
      toolsMap[slug] ?? {
        href: "/tools/url-qr-code",
        title: "QR Code Generator",
        description: "Create a QR code for your business.",
      }
  );
  const useCaseStrings = Array.isArray(page.useCases) ? page.useCases : [];

  return (
    <IndustryPageTemplate
      title={page.title}
      description={page.metaDescription}
      eyebrow={page.name}
      intro={page.subheadline || page.headline}
      pageType="commercial"
      cluster="industry"
      useCase={page.slug}
      breadcrumbs={[
        { name: "Home", url: "/" },
        { name: "QR Codes by Industry", url: "/qr-code-for" },
        { name: page.title, url: `/qr-code-for/${page.slug}` },
      ]}
      answer={page.headline}
      whenToUse={useCaseStrings.slice(0, 3)}
      comparisonItems={[
        { label: "Update destination after printing", value: true, text: "Dynamic QR codes" },
        { label: "Track scans by date, device & location", value: true, text: "Built-in analytics" },
        { label: "No app needed to scan", value: true, text: "Works with native camera" },
      ]}
      howToSteps={page.placementTips.slice(0, 3)}
      primaryCta={primaryCta}
      secondaryCta={secondaryCta}
      workflowTitle={`How ${page.name} Use QR Codes`}
      workflowIntro={`Practical QR code workflows built around how ${page.name.toLowerCase()} actually operate.`}
      workflowCards={useCaseStrings.slice(0, 3).map((useCase, index) => ({
        title: `Workflow ${index + 1}`,
        description: useCase,
      }))}
      checklistTitle={`${page.name} QR Code Checklist`}
      checklist={page.placementTips}
      supportLinks={supportLinks}
      faq={page.faq}
      heroImage={page.heroImage}
      heroImageAlt={getHeroImageAlt(page)}
      statistics={page.statistics}
      benefits={page.benefits}
    />
  );
}
