import { getServerUser } from "@/lib/auth/server";
import BuyerDashboard from "@/components/dashboard/BuyerDashboard";
import SellerDashboard from "@/components/dashboard/SellerDashboard";
import TransporterDashboard from "@/components/dashboard/TransporterDashboard";
import SellerWelcomeDialog from "@/components/dashboard/SellerWelcomeDialog";
import { BuyerSellerDashboard } from "@/components/dashboard/DashboardMode";

/**
 * Picks the dashboard variant on the server.
 *
 * The role lives in the same cookie `middleware.ts` already reads, so branching
 * on it does not have to wait for `AuthContext` to hydrate — which is what the
 * "Loading…" spinner here used to be waiting for. The three dashboards remain
 * client components; only the choice between them moved.
 */
export default async function DashboardPage() {
  const user = await getServerUser();

  /* A buyer approved as a seller works both sides of the marketplace: the
     sidebar's Buying / Selling switch picks which dashboard shows. */
  if (user?.is_buyer_and_seller) {
    return (
      <>
        <SellerWelcomeDialog />
        <BuyerSellerDashboard />
      </>
    );
  }

  if (user?.type === "seller") {
    return (
      <>
        <SellerWelcomeDialog />
        <SellerDashboard />
      </>
    );
  }
  if (user?.type === "transporter") return <TransporterDashboard />;
  return <BuyerDashboard />;
}
