import { DashboardPage } from "@/components/layout/dashboard-page";
import { getDashboardRole } from "@/lib/auth/dashboard-role";
import { redirect } from "next/navigation";
import { DashboardView } from "@/features/dashboard/dashboard-view";
import { isManager, type PreviewRole } from "@/lib/preview";
async function Page({
  searchParams,
  role,
}: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
  role: PreviewRole;
}) {
  const params = await searchParams;
  if (role === "investor" || isManager(role))
    redirect(`/locations?role=${role}`);
  return (
    <DashboardView
      empty={params.state === "empty"}
      failed={params.state === "error"}
    />
  );
}

export default async function DashboardRoute(props: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}) {
  const query = await props.searchParams;
  const role = await getDashboardRole(
    Array.isArray(query.role) ? query.role[0] : query.role,
  );
  return (
    <DashboardPage role={role}>
      <Page searchParams={props.searchParams} role={role} />
    </DashboardPage>
  );
}
