import { redirect } from "next/navigation";

import SignInForm from "@/components/admin/SignInForm";
import { readSession } from "@/lib/api/admin-server";
import { authDevBypassEnabled, googleClientId } from "@/lib/public-config";

export const dynamic = "force-dynamic";

export const metadata = {
  title: "Sign in — RSVP Admin",
  robots: { index: false, follow: false },
};

export default async function SignInPage() {
  // Already signed in — skip the form rather than letting someone re-authenticate over
  // a live session.
  if (await readSession()) redirect("/admin");

  // Null when unset or still a placeholder — task 1.10 supplies the real client. Treating a
  // placeholder as configured would render a Google button that fails with an opaque error.
  const clientId = googleClientId();
  const devBypass = authDevBypassEnabled();

  return (
    <main className="flex min-h-dvh items-center justify-center bg-stone-50 px-4">
      <div className="w-full max-w-sm rounded-2xl border border-stone-200 bg-white p-8 shadow-sm">
        <h1 className="text-xl font-semibold tracking-tight text-stone-900">RSVP Admin</h1>
        <p className="mt-2 text-sm text-stone-600">
          Sign in with your Google account, or with the username and password a super admin
          gave you.
        </p>

        <SignInForm clientId={clientId} devBypassEnabled={devBypass} />

        <p className="mt-6 border-t border-stone-100 pt-4 text-xs text-stone-500">
          Signing in with Google for the first time creates an account and puts it in the
          approval queue — a super admin decides whether it gets in, and what it may see.
          Username-and-password accounts are only ever created by a super admin.
        </p>
      </div>
    </main>
  );
}
