"use client";

import { useSearchParams } from "next/navigation";
import { useQuery } from "@tanstack/react-query";
import { useTranslations, useLocale } from "next-intl";
import { Link } from "@/i18n/routing";
import { apiGet } from "@/lib/api/client";
import type { QueueBoard } from "@/lib/api/types";
import { useAuthStore } from "@/lib/stores/auth.store";
import { formatNumber } from "@/lib/format";

export function QueueDisplayScreen() {
  const t = useTranslations("queue");
  const locale = useLocale();
  const numberLocale = locale === "en" ? "en-US" : "fa-IR";
  const cafeId = useAuthStore((s) => s.user?.cafeId);
  const searchParams = useSearchParams();
  const branchId = searchParams.get("branchId");
  const branchQuery = branchId ? `?branchId=${encodeURIComponent(branchId)}` : "";

  const { data: board } = useQuery({
    queryKey: ["queue-today", cafeId, branchId, "display"],
    queryFn: () => apiGet<QueueBoard>(`/api/cafes/${cafeId}/queue/today${branchQuery}`),
    enabled: !!cafeId,
    refetchInterval: 5_000,
  });

  const waiting = board?.tickets.filter((x) => x.status === "Waiting") ?? [];
  const nowServing = board?.nowServing;

  return (
    <div className="flex min-h-svh flex-col bg-neutral-950 text-white">
      <div className="flex items-center justify-between border-b border-white/10 px-6 py-3">
        <p className="text-sm font-medium uppercase tracking-[0.12em] text-white/60">
          {t("title")} · {t("displayMode")}
        </p>
        <Link
          href="/queue"
          className="text-xs text-white/50 underline-offset-2 hover:text-white hover:underline"
        >
          {t("exitDisplay")}
        </Link>
      </div>

      <div className="flex flex-1 flex-col items-center justify-center gap-10 px-6 py-8">
        <div className="text-center">
          <p className="text-lg font-medium uppercase tracking-[0.14em] text-amber-400/90">
            {t("nowServing")}
          </p>
          <p className="mt-2 text-[min(28vw,12rem)] font-bold tabular-nums leading-none text-white">
            {nowServing != null ? formatNumber(nowServing, numberLocale) : "—"}
          </p>
        </div>

        <div className="grid w-full max-w-4xl gap-6 sm:grid-cols-2">
          <div className="rounded-2xl border border-white/10 bg-white/5 px-6 py-5 text-center">
            <p className="text-xs uppercase tracking-wide text-white/50">{t("lastIssued")}</p>
            <p className="mt-2 text-4xl font-semibold tabular-nums">
              {formatNumber(board?.lastIssued ?? 0, numberLocale)}
            </p>
          </div>
          <div className="rounded-2xl border border-white/10 bg-white/5 px-6 py-5 text-center">
            <p className="text-xs uppercase tracking-wide text-white/50">{t("displayWaitingLabel")}</p>
            <p className="mt-2 text-4xl font-semibold tabular-nums text-amber-300">
              {formatNumber(board?.waitingCount ?? 0, numberLocale)}
            </p>
          </div>
        </div>

        {waiting.length > 0 ? (
          <div className="w-full max-w-3xl">
            <p className="mb-3 text-center text-xs uppercase tracking-wide text-white/40">
              {t("displayUpNext")}
            </p>
            <div className="flex flex-wrap justify-center gap-3">
              {waiting.slice(0, 12).map((ticket) => (
                <span
                  key={ticket.id}
                  className="rounded-xl border border-amber-500/30 bg-amber-500/10 px-5 py-3 text-2xl font-bold tabular-nums"
                >
                  {formatNumber(ticket.number, numberLocale)}
                </span>
              ))}
            </div>
          </div>
        ) : null}
      </div>
    </div>
  );
}
