/** توضیحات، روش مصرف، ترکیبات و مشخصات محصول (آکاردئون بومی HTML) */
import type { Brand, Product, SkinType } from "@/types/catalog";
import { faDigits } from "@/lib/format";

export function ProductDetails({ product, brand, skinTypes }: { product: Product; brand: Brand; skinTypes: SkinType[] }) {
  const sizes = [...new Set(product.variants.map((v) => v.size).filter(Boolean))].join("، ");
  const specs: [string, string][] = [
    ["برند", `${brand.name} (${brand.nameEn})`],
    ["کشور سازنده", product.madeIn],
    ...(sizes ? [["حجم / وزن", sizes] as [string, string]] : []),
    ...(skinTypes.length ? [["مناسب برای", skinTypes.map((s) => `پوست ${s.name}`).join("، ")] as [string, string]] : []),
    ...(product.ircCode ? [["شماره مجوز بهداشتی (IRC)", faDigits(product.ircCode)] as [string, string]] : []),
    ["کد کالا", faDigits(product.variants[0].sku.replace(/-\d+$/, ""))],
  ];

  const sections = [
    { title: "درباره محصول", body: <p>{product.description}</p>, open: true },
    { title: "روش مصرف", body: <p>{product.howToUse}</p> },
    { title: "ترکیبات", body: <p>{product.ingredients}</p> },
    {
      title: "مشخصات",
      body: (
        <dl className="divide-y divide-line">
          {specs.map(([k, v]) => (
            <div key={k} className="flex justify-between gap-4 py-2.5">
              <dt className="text-muted">{k}</dt>
              <dd className="text-end font-medium">
                <bdi>{v}</bdi>
              </dd>
            </div>
          ))}
        </dl>
      ),
      open: true,
    },
  ];

  return (
    <section id="details" className="scroll-mt-32">
      <h2 className="section-title mb-2">جزئیات</h2>
      {sections.map((s) => (
        <details key={s.title} open={s.open} className="group border-b border-line [&_summary::-webkit-details-marker]:hidden">
          <summary className="flex cursor-pointer list-none items-center justify-between py-4 text-sm font-bold">
            {s.title}
            <span className="text-xl leading-none font-light transition-transform group-open:rotate-45">+</span>
          </summary>
          <div className="pb-5 text-sm leading-8 text-ink-soft">{s.body}</div>
        </details>
      ))}
    </section>
  );
}
