import { Dashboard } from "@/components/dashboard";
import { redirect } from "next/navigation";
import { getCurrentAppUser } from "../lib/current-user";
import { isAdminUser } from "../lib/auth-store";
import { headers } from "next/headers";
import { verifyRuntimeLicense } from "../lib/runtime-license";
import { LicenseBlocked } from "../components/license-blocked";

export const dynamic = "force-dynamic";

export default async function Home() {
  const requestHeaders = await headers();
  const license = await verifyRuntimeLicense(requestHeaders.get("host") || "localhost");
  if (!license.valid) return <LicenseBlocked reason={license.reason} domain={license.domain} installationId={license.installationId}/>;
  const user = await getCurrentAppUser();
  if (!user) redirect("/login?portal=admin");
  if (!isAdminUser(user)) redirect("/customer");
  return <Dashboard user={{ name: user.name, email: user.email }} />;
}
