"use client";

import { Suspense } from "react";
import { useRouter, useSearchParams } from "next/navigation";
import { AuthForm } from "@/components/auth/AuthForm";

function isSafeRedirect(url: string | null): string {
  if (!url || !url.startsWith("/") || url.startsWith("//") || url.includes("://")) {
    return "/account";
  }
  return url;
}

function LoginContent() {
  const router = useRouter();
  const searchParams = useSearchParams();
  const redirect = isSafeRedirect(searchParams.get("redirect"));

  return (
    <main className="mx-auto max-w-md px-6 py-16">
      <AuthForm onSuccess={() => router.push(redirect)} />
    </main>
  );
}

export default function LoginPage() {
  return (
    <Suspense>
      <LoginContent />
    </Suspense>
  );
}
