// app/page.tsx
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import SignIn from "@/app/auth/sign-in/sign-in";
import SignUp from "@/app/auth/sign-up/sign-up";
import { isJwtValid } from "@/lib/jwt";

type SearchParams = { [key: string]: string | string[] | undefined };

export default async function HomePage({
  searchParams,
}: {
  searchParams?: SearchParams;
}) {
  // If already signed in with a VALID token, go straight to the app
  const ck = await cookies();
  const at = ck.get("ma_at")?.value;
  if (isJwtValid(at)) redirect("/portal");

  const reauth = searchParams?.reauth === "1";

  return (
    <main className="mx-auto max-w-5xl px-4 py-12">
    {reauth && (
      <p className="mb-6 rounded-md border bg-yellow-50 p-3 text-sm text-yellow-900">
      Your session expired. Please sign in again.
      </p>
    )}

    <section className="mb-10 text-center">
    <h1 className="text-3xl font-bold tracking-tight">MakeArmy</h1>
    <p className="mt-2 text-base text-muted-foreground">
    Free to use. Manage laser rigs, settings, and projects—all in one
    place.
    </p>
    </section>

    <section className="grid gap-6 md:grid-cols-2">
    <SignUp nextPath="/portal" />
    <SignIn nextPath="/portal" reauth={reauth} />
    </section>

    <section className="mt-8 text-center text-xs text-muted-foreground">
    <p>This is the beta build v0.1.1 - this site is an active BETA. Some features may not be available or work as intended. If you're experiencing issues please report them here: https://forge.makearmy.io/makearmy/makearmy-app/issues </p>
    <p>PRIVACY: We only use cookies strictly necessary to operate the site (e.g., your sign-in session). We do not store user data or telemetry other than what you provide and never share or sell data to third parties. Ever.</p>
    </section>
    </main>
  );
}
