import { headers } from "next/headers";
import { auth } from "@/lib/auth/server";
import { db, pantryItems, eq, asc } from "@epicure/db";
import { PrintTrigger } from "@/components/recipe/print-trigger";
import { getMessages, formatMessage } from "@/lib/i18n/server";
import { formatQuantity, hasQuantity } from "@/lib/fractions";

export default async function PantryPrintPage() {
  const session = await auth.api.getSession({ headers: await headers() });
  if (!session) return null;
  const m = getMessages((session.user as { locale?: string }).locale);

  const items = await db.query.pantryItems.findMany({
    where: eq(pantryItems.userId, session.user.id),
    orderBy: asc(pantryItems.rawName),
  });

  const now = new Date();

  return (
    <>
      <style>{`
        @media print {
          body { margin: 0; }
          .no-print { display: none !important; }
        }
        body {
          font-family: system-ui, sans-serif;
          color: #1a1a1a;
          max-width: 680px;
          margin: 40px auto;
          padding: 0 24px;
          font-size: 14px;
          line-height: 1.6;
        }
        h1 { font-size: 1.8em; margin: 0 0 4px; }
        .subtitle { color: #666; font-size: 0.9em; margin-bottom: 24px; }
        table { width: 100%; border-collapse: collapse; }
        th { text-align: left; font-size: 0.8em; text-transform: uppercase; letter-spacing: 0.06em; color: #888; padding: 4px 8px 4px 0; border-bottom: 1px solid #ccc; }
        td { padding: 6px 8px 6px 0; border-bottom: 1px dotted #eee; font-size: 0.9em; vertical-align: top; }
        tr:last-child td { border-bottom: none; }
        .expiry-soon { color: #b45309; }
        .expiry-expired { color: #dc2626; }
        footer { margin-top: 40px; font-size: 0.75em; color: #aaa; text-align: center; }
        .print-btn {
          position: fixed; top: 16px; right: 16px; padding: 8px 16px;
          background: #18181b; color: white; border: none; border-radius: 6px;
          cursor: pointer; font-size: 13px;
        }
        .print-btn:hover { background: #3f3f46; }
      `}</style>

      <PrintTrigger />

      <h1>{m.pantry.title}</h1>
      <p className="subtitle">{formatMessage(items.length === 1 ? m.pantry.printItemCountSingular : m.pantry.printItemCountPlural, { count: items.length })}</p>

      {items.length === 0 ? (
        <p style={{ color: "#888" }}>{m.pantry.noItems}</p>
      ) : (
        <table>
          <thead>
            <tr>
              <th>{m.pantry.colItem}</th>
              <th>{m.pantry.colQuantity}</th>
              <th>{m.pantry.colExpires}</th>
            </tr>
          </thead>
          <tbody>
            {items.map((item) => {
              const exp = item.expiresAt ? new Date(item.expiresAt) : null;
              const daysLeft = exp ? Math.ceil((exp.getTime() - now.getTime()) / 86400000) : null;
              const expiryClass = daysLeft === null ? "" : daysLeft < 0 ? "expiry-expired" : daysLeft <= 3 ? "expiry-soon" : "";
              return (
                <tr key={item.id}>
                  <td>{item.rawName}</td>
                  <td>{[hasQuantity(item.quantity) ? formatQuantity(parseFloat(item.quantity!)) : null, item.unit].filter(Boolean).join(" ") || "—"}</td>
                  <td className={expiryClass}>
                    {exp
                      ? daysLeft !== null && daysLeft < 0
                        ? formatMessage(m.pantry.expiredDaysAgo, { days: Math.abs(daysLeft) })
                        : exp.toLocaleDateString("en", { month: "short", day: "numeric", year: "numeric" })
                      : "—"}
                  </td>
                </tr>
              );
            })}
          </tbody>
        </table>
      )}

      <footer>{m.print.footer}</footer>
    </>
  );
}
