"use client";

import { useState } from "react";
import type { Product } from "@/lib/api";

/**
 * Product details tabs: Description · Ingredients · How to use · Product details.
 * Ingredients / How to use only appear when the catalogue has label copy for them —
 * nothing is invented. Without a written description, the Description tab shows the
 * product's factual details instead of template marketing text.
 */
export default function PdpTabs({ p }: { p: Product }) {
  const hasDesc = !!(p.desc && p.desc.replace(/<[^>]+>/g, "").trim());
  const tabs = [
    { id: "desc", label: "Description", show: true },
    { id: "ingredients", label: "Ingredients", show: !!p.ingredients },
    { id: "use", label: "How to use", show: !!p.howToUse },
    { id: "details", label: "Product details", show: true },
  ].filter((t) => t.show);
  const [tab, setTab] = useState(tabs[0].id);

  const facts: [string, string | null | undefined][] = [
    ["Product", p.name],
    ["Brand", p.brand],
    ["Category", [p.parentCategoryLabel, p.categoryLabel].filter(Boolean).join(" › ")],
    ["Size / pack", p.size],
    ["Variant", p.variant],
    ["SKU", p.sku],
    ["Manufacturer code", p.supplierCode],
    ["Case size", p.caseSize ? `${p.caseSize} units` : "On request"],
    ["Minimum order", p.minOrder ? `${p.minOrder} units` : "1 unit"],
  ];

  return (
    <div className="pb-pdp-tabs">
      <div className="pb-pdp-tablist" role="tablist" aria-label="Product information">
        {tabs.map((t) => (
          <button
            key={t.id}
            role="tab"
            id={`tab-${t.id}`}
            aria-selected={tab === t.id}
            aria-controls={`panel-${t.id}`}
            className={`pb-pdp-tab${tab === t.id ? " is-active" : ""}`}
            onClick={() => setTab(t.id)}
            type="button"
          >
            {t.label}
          </button>
        ))}
      </div>

      <div role="tabpanel" id={`panel-${tab}`} aria-labelledby={`tab-${tab}`} className="pb-pdp-panel">
        {tab === "desc" &&
          (hasDesc ? (
            <div className="pb-pdp-rich" dangerouslySetInnerHTML={{ __html: p.desc! }} />
          ) : (
            <p>
              {p.name} from {p.brand}
              {p.categoryLabel ? `, in our ${p.categoryLabel.toLowerCase()} range` : ""}. Supplied authentic to pharmacies,
              clinics and retailers across the UAE at trade prices. For the full product information sheet, ask your
              Awafi account manager.
            </p>
          ))}
        {tab === "ingredients" && <p className="pb-pdp-pre">{p.ingredients}</p>}
        {tab === "use" && <p className="pb-pdp-pre">{p.howToUse}</p>}
        {tab === "details" && (
          <dl className="pb-pdp-facts">
            {facts
              .filter(([, v]) => v)
              .map(([k, v]) => (
                <div key={k}>
                  <dt>{k}</dt>
                  <dd>{v}</dd>
                </div>
              ))}
          </dl>
        )}
      </div>
    </div>
  );
}
