import type { Metadata } from "next";
import { redirect } from "next/navigation";
import { BrandLogo } from "@/components/brand/brand-logo";
import { safeAdminRedirect } from "@/lib/security/safe-redirect";
import { getCurrentSession } from "@/server/auth/current-user";
import { LoginForm } from "@/app/admin/login/login-form";

export const metadata: Metadata = {
  title: "Iniciar sesión",
  robots: { index: false, follow: false },
};

type LoginPageProps = {
  searchParams: Promise<{ next?: string; expired?: string; logout?: string }>;
};

export default async function AdminLoginPage({ searchParams }: LoginPageProps) {
  const params = await searchParams;
  const session = await getCurrentSession();
  if (session) redirect(safeAdminRedirect(params.next));

  const next = params.next ? safeAdminRedirect(params.next) : null;

  return (
    <div className="grid min-h-dvh md:grid-cols-2">
      {/* Izquierda: fotografía automotriz premium (pendiente de material oficial). */}
      <div aria-hidden className="hidden items-end bg-chesco-graphite p-10 md:flex">
        <p className="text-xs uppercase tracking-widest text-chesco-metallic">
          Fotografía automotriz — pendiente
        </p>
      </div>

      <div className="flex items-center justify-center bg-chesco-black px-6 py-12">
        <div className="w-full max-w-sm">
          <div className="mb-10 flex justify-center">
            <BrandLogo variant="logoWhite" height={88} priority />
          </div>
          <div className="rounded-chesco-lg bg-chesco-white p-6 text-chesco-text sm:p-8">
            <h1 className="font-display text-xl font-bold">Panel administrativo</h1>
            {params.expired && (
              <p className="mt-2 text-sm text-chesco-text-muted">
                Tu sesión terminó. Vuelve a ingresar.
              </p>
            )}
            {params.logout && (
              <p className="mt-2 text-sm text-chesco-text-muted">Cerraste sesión correctamente.</p>
            )}
            <div className="mt-6">
              <LoginForm next={next} />
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
