import { cookies } from "next/headers";
import { verifyToken } from "@/lib/auth/jwt";
import { getMachineId } from "@/shared/utils/machine";
import EndpointPageClient from "./endpoint/EndpointPageClient";

export const dynamic = "force-dynamic";

async function getAdminData() {
  try {
    const cookieStore = await cookies();
    const token = cookieStore.get("auth_token")?.value;
    if (!token) return { isAdmin: false };
    const payload = await verifyToken(token);
    if (!payload) return { isAdmin: false };
    return { isAdmin: payload.role === "admin" };
  } catch {
    return { isAdmin: false };
  }
}

export default async function DashboardPage() {
  const machineId = await getMachineId();
  const { isAdmin } = await getAdminData();
  return <EndpointPageClient machineId={machineId} isAdmin={isAdmin} />;
}
