import { cookies } from "next/headers";
import { redirect } from "next/navigation";

import { LoginContainer, LoginSuccessContainer } from "@/components/core/login-form";

export interface HomeParams {
  returnTo?: string;
}

export default async function Home({ 
  searchParams 
}: { 
  searchParams: Promise<HomeParams> 
}) {
  const params = await searchParams;
  const cookieStore = await cookies();
  const token = cookieStore.get("token");

  // If token exists and we came from a redirect, go back
  if (token && params.returnTo) {
    redirect(params.returnTo);
  }

  if (token) {
    return (
      <div className="flex justify-center items-center h-screen">
        <LoginSuccessContainer applicationName="SecNex" />
      </div>
    );
  }

  return (
    <div className="flex justify-center items-center h-screen">
      <LoginContainer applicationName="SecNex" />
    </div>
  );
}