import type { CheckinStatusData } from "@alqove/api-client";
import { checkinApi } from "@/lib/checkin/api";
import { StatusPoller } from "./StatusPoller";

// Status changes off-page (staff actions); don't cache the initial fetch.
export const dynamic = "force-dynamic";

interface StatusPageProps {
  params: Promise<{ token: string; statusToken: string }>;
}

function NotFound() {
  return (
    <main className="flex flex-1 items-center justify-center px-6 py-16">
      <div className="max-w-sm text-center">
        <div className="mb-4 text-4xl" aria-hidden="true">
          🔎
        </div>
        <h1 className="text-xl font-semibold text-slate-900">
          We couldn&apos;t find that check-in
        </h1>
        <p className="mt-3 text-sm text-slate-600">
          This status link may be incorrect or expired. If you just checked in,
          try the link from your confirmation again.
        </p>
      </div>
    </main>
  );
}

/**
 * Public status page, reachable cold from the SMS deep link with only the
 * status token — no auth, no prior client state. Server-fetches the initial
 * status for instant paint, then hands off to the client poller.
 */
export default async function StatusPage({ params }: StatusPageProps) {
  const { statusToken } = await params;

  let initialStatus: CheckinStatusData;
  try {
    const res = await checkinApi.getStatus(statusToken);
    initialStatus = res.data;
  } catch {
    // 404 (and, defensively, any other failure) surfaces as "not found" so a
    // guessed/expired token never leaks anything.
    return <NotFound />;
  }

  return (
    <main className="flex-1 px-6 py-10">
      <StatusPoller statusToken={statusToken} initialStatus={initialStatus} />
    </main>
  );
}
