import { redirect } from "next/navigation";
import { LoginShell } from "@/components/login-shell";
import { getSafeRedirectPath, hasDemoAccess } from "@/lib/app-auth";
import { getDemoAppConfig } from "@/lib/demo-content";

const errorMessages: Record<string, string> = {
  invalid_password: "Le mot de passe demo est invalide.",
};

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

export default async function LoginPage({ searchParams }: LoginPageProps) {
  if (await hasDemoAccess()) {
    redirect("/");
  }

  const appConfig = await getDemoAppConfig();
  const { error, next } = await searchParams;

  return (
    <LoginShell
      appConfig={appConfig}
      initialError={error ? errorMessages[error] ?? null : null}
      nextPath={getSafeRedirectPath(next)}
    />
  );
}
