import { permanentRedirect } from "next/navigation";

export const dynamic = "force-dynamic";

type PageProps = {
  searchParams?: Promise<Record<string, string | string[] | undefined>>;
};

/**
 * /account was the Plex linking page before the profile existed.
 *
 * Kept as a redirect rather than deleted because Plex used to be told to return
 * the user HERE after signing in, so a bookmark or a PIN round trip started
 * before the move should land on the profile rather than a 404.
 *
 * The query string is carried across so nothing is silently dropped on the way.
 * Finishing a link no longer depends on it -- that happens in
 * /api/plex/callback now -- but a redirect that eats its parameters is a trap
 * for whatever gets added to this page later.
 */
export default async function AccountPage({ searchParams }: PageProps) {
  const resolved = (await searchParams) ?? {};
  const params = new URLSearchParams();

  for (const [key, value] of Object.entries(resolved)) {
    for (const single of Array.isArray(value) ? value : value === undefined ? [] : [value]) {
      params.append(key, single);
    }
  }

  const query = params.toString();
  permanentRedirect(query ? `/profile?${query}` : "/profile");
}
