import Link from "next/link";
import { getMenuItems } from "@/lib/menuStore";
import { AdminNav } from "@/components/admin/AdminNav";
import type { MenuItem } from "@/lib/types";

export const dynamic = "force-dynamic";

const CATEGORY_ORDER: MenuItem["category"][] = ["antipasti", "pizza", "pasta", "secondi", "dolci", "drinks"];
const CATEGORY_LABELS: Record<MenuItem["category"], string> = {
  antipasti: "Starters",
  pizza: "Pizza",
  pasta: "Pasta",
  secondi: "Mains",
  dolci: "Dolci",
  drinks: "Drinks",
};

export default async function AdminMenuPage() {
  const items = await getMenuItems();

  return (
    <div>
      <AdminNav />
      <div className="mx-auto max-w-5xl px-6 py-10">
        <div className="flex items-center justify-between">
          <div>
            <h1 className="text-2xl font-semibold">Menu</h1>
            <p className="mt-1 text-sm text-ink/60">{items.length} items</p>
          </div>
          <Link
            href="/admin/menu/new"
            className="rounded-lg bg-tomato px-4 py-2 text-sm font-medium text-white hover:bg-tomato-dark"
          >
            + New dish
          </Link>
        </div>

        {CATEGORY_ORDER.map((category) => {
          const categoryItems = items.filter((i) => i.category === category);
          if (categoryItems.length === 0) return null;

          return (
            <section key={category} className="mt-8">
              <h2 className="text-sm font-semibold uppercase tracking-wide text-ink/50">
                {CATEGORY_LABELS[category]} · {categoryItems.length}
              </h2>
              <div className="mt-3 overflow-hidden rounded-xl border border-ink/10 bg-white">
                <table className="w-full text-left text-sm">
                  <tbody className="divide-y divide-ink/10">
                    {categoryItems.map((item) => (
                      <tr key={item.id}>
                        <td className="px-4 py-3">
                          <span className={item.available ? "" : "text-ink/40 line-through"}>{item.name.en}</span>
                          {item.signature && <span className="ml-2 rounded-full bg-olive/15 px-2 py-0.5 text-xs text-olive">Signature</span>}
                          {item.chefRecommended && <span className="ml-2 rounded-full bg-tomato/15 px-2 py-0.5 text-xs text-tomato">Chef&apos;s pick</span>}
                        </td>
                        <td className="px-4 py-3 text-ink/60">
                          {[item.vegetarian && "Veg", item.vegan && "Vegan", item.spicy && "Spicy"].filter(Boolean).join(" · ")}
                        </td>
                        <td className="px-4 py-3 font-medium">€{item.price.toFixed(2)}</td>
                        <td className="px-4 py-3 text-right">
                          <Link href={`/admin/menu/${item.id}`} className="text-tomato hover:underline">
                            Edit
                          </Link>
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            </section>
          );
        })}
      </div>
    </div>
  );
}
