import { getLocale, getTranslations } from "next-intl/server";
import { getMenuItems } from "@/lib/menuStore";
import { localize } from "@/lib/localize";
import type { MenuItem } from "@/lib/types";
import { Reveal } from "@/components/Reveal";

const CATEGORY_ORDER: MenuItem["category"][] = ["antipasti", "pizza", "pasta", "secondi", "dolci", "drinks"];

export const dynamic = "force-dynamic";

export default async function MenuPage() {
  const t = await getTranslations("menuPage");
  const tRoot = await getTranslations();
  const locale = await getLocale();
  const menuItems = await getMenuItems();

  const categories = CATEGORY_ORDER.filter((category) =>
    menuItems.some((item) => item.category === category && item.available)
  );

  return (
    <div>
      <div className="mx-auto max-w-3xl px-6 pt-10">
        <Reveal className="text-center">
          <p className="eyebrow text-brass">La Tavola Italiana</p>
          <h1 className="mt-3 font-serif text-4xl italic text-ivory">{t("title")}</h1>
          <p className="mt-2 text-ivory-dim">{t("subtitle")}</p>
        </Reveal>
      </div>

      <nav className="sticky top-20 z-30 mt-6 border-y border-ivory/10 bg-espresso/95 py-3 backdrop-blur supports-[backdrop-filter]:bg-espresso/80 md:top-28">
        <div className="mx-auto flex max-w-3xl justify-center">
          <div className="scrollbar-none flex min-w-0 gap-2 overflow-x-auto px-6">
            {categories.map((category) => (
              <a
                key={category}
                href={`#${category}`}
                className="eyebrow flex shrink-0 items-center whitespace-nowrap rounded-full border border-ivory/15 px-4 py-2.5 text-ivory-dim transition hover:border-tomato/50 hover:text-ivory"
              >
                {t(`categories.${category}`)}
              </a>
            ))}
          </div>
        </div>
      </nav>

      <div className="mx-auto max-w-3xl px-6 pb-24">
        {categories.map((category, catIndex) => {
          const items = menuItems.filter((item) => item.category === category && item.available);
          const subcategories = Array.from(new Set(items.map((i) => i.subcategory ?? "")));

          return (
            <section key={category} id={category} className="mt-16 scroll-mt-56 first:mt-10">
              <Reveal className="relative mb-6 text-center">
                <span
                  className="pointer-events-none absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 select-none font-serif text-8xl italic text-ivory/[0.04]"
                  aria-hidden
                >
                  {String(catIndex + 1).padStart(2, "0")}
                </span>
                <h2 className="relative font-serif text-3xl italic text-tomato">{t(`categories.${category}`)}</h2>
              </Reveal>

              {subcategories.map((sub) => (
                <div key={sub} className="mt-8 first:mt-0">
                  {sub && (
                    <h3 className="eyebrow mb-3 text-brass/80">{sub.replace(/-/g, " ")}</h3>
                  )}
                  <ul className="space-y-5">
                    {items
                      .filter((i) => (i.subcategory ?? "") === sub)
                      .map((item) => (
                        <li key={item.id} className="flex gap-4">
                          {item.photo && (
                            <img
                              src={item.photo}
                              alt=""
                              loading="lazy"
                              className="h-16 w-16 shrink-0 rounded-lg object-cover"
                            />
                          )}
                          <div className="min-w-0 flex-1">
                            <div className="flex items-baseline gap-2">
                              <p className="font-serif text-lg italic text-ivory">
                                {localize(item.name, locale)}
                                {item.chefRecommended && (
                                  <span className="eyebrow ml-2 align-middle text-olive">{t("chefRecommended")}</span>
                                )}
                              </p>
                              <span className="leader text-ivory-dim" />
                              <span className="whitespace-nowrap text-base font-medium tabular-nums text-ivory">
                                €{item.price.toFixed(2)}
                              </span>
                            </div>
                            {item.description && (
                              <p className="mt-1 text-sm italic text-ivory-dim/80">{localize(item.description, locale)}</p>
                            )}
                            {(item.vegetarian || item.vegan || item.spicy) && (
                              <div className="mt-1.5 flex flex-wrap gap-x-3 gap-y-1 text-xs text-ivory-dim/50">
                                {item.vegetarian && <span>{t("vegetarian")}</span>}
                                {item.vegan && <span>{t("vegan")}</span>}
                                {item.spicy && <span className="text-tomato/80">{t("spicy")}</span>}
                              </div>
                            )}
                          </div>
                        </li>
                      ))}
                  </ul>
                </div>
              ))}
            </section>
          );
        })}

        <p className="mx-auto mt-16 max-w-xl text-center text-xs leading-relaxed text-ivory-dim/60">
          {tRoot("allergenDisclaimer")}
        </p>
      </div>
    </div>
  );
}
