import { redirect } from "next/navigation";

/**
 * Legacy `/auth` route — kept as a redirect for back-compat. NextAuth's
 * `pages.signIn`, the VibnCode desktop `vibncode://` SSO deep-link, and any old
 * links still point here. `?new=1` maps to /signup; everything else (including
 * `?vibncode=true`) carries over to /signin.
 */
export default async function AuthRedirect({
  searchParams,
}: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}) {
  const sp = (await searchParams) ?? {};
  const isNew = sp.new !== undefined;

  const params = new URLSearchParams();
  for (const [key, value] of Object.entries(sp)) {
    if (key === "new") continue;
    if (typeof value === "string") params.set(key, value);
    else if (Array.isArray(value) && value[0] != null)
      params.set(key, value[0]);
  }
  const qs = params.toString();
  redirect(`${isNew ? "/signup" : "/signin"}${qs ? `?${qs}` : ""}`);
}
