import { redirect } from "next/navigation"
import {
  getAdminUserFromCookies,
  isAdminCredentialLoginConfigured,
  isAdminUiEnabled,
} from "@/lib/server/admin-auth"
import { PublicPageHeader, PublicSurface } from "@/components/public-surface"

type PageProps = {
  searchParams: Promise<{
    error?: string
  }>
}

export default async function SignInPage({ searchParams }: PageProps) {
  if (!isAdminUiEnabled()) {
    redirect("/")
  }

  const adminUser = await getAdminUserFromCookies()
  if (adminUser) {
    redirect("/admin")
  }

  const params = await searchParams
  const errorMessage =
    params.error === "invalid"
      ? "That email or password was not accepted."
      : params.error === "config"
        ? "Admin sign-in is not fully configured yet."
        : ""

  return (
    <div className="px-4 py-8 md:py-12">
      <div className="mx-auto flex min-h-[calc(100dvh-7rem)] max-w-3xl items-start justify-center md:items-center">
        <div className="w-full max-w-xl space-y-8">
          <PublicPageHeader
            align="center"
            eyebrow="Customer Flow"
            title="Admin Sign-In"
            description="This area is reserved for Rocky Mountain Vending admins."
          />

          <PublicSurface className="p-6 text-center md:p-8">
            {isAdminCredentialLoginConfigured() ? (
              <form
                action="/api/admin/auth/login"
                method="post"
                className="mx-auto max-w-sm space-y-4 text-left"
              >
                <div className="space-y-2">
                  <label className="text-sm font-medium" htmlFor="email">
                    Email
                  </label>
                  <input
                    id="email"
                    name="email"
                    type="email"
                    autoComplete="email"
                    className="flex h-11 w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
                    placeholder="matt@rockymountainvending.com"
                    required
                  />
                </div>
                <div className="space-y-2">
                  <label className="text-sm font-medium" htmlFor="password">
                    Password
                  </label>
                  <input
                    id="password"
                    name="password"
                    type="password"
                    autoComplete="current-password"
                    className="flex h-11 w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
                    required
                  />
                </div>
                {errorMessage ? (
                  <p className="text-sm text-destructive">{errorMessage}</p>
                ) : null}
                <button className="inline-flex h-11 w-full items-center justify-center rounded-md bg-primary px-4 text-sm font-medium text-primary-foreground">
                  Sign in
                </button>
              </form>
            ) : (
              <>
                <h2 className="text-2xl font-semibold">
                  Admin sign-in is not configured
                </h2>
                <p className="mt-3 text-sm text-muted-foreground">
                  Enable admin UI, Convex, and staging credentials before using
                  this area.
                </p>
              </>
            )}
          </PublicSurface>
        </div>
      </div>
    </div>
  )
}
