import { cookies } from "next/headers";
import { verifyToken } from "@/lib/auth/jwt";
import PlanClient from "./PlanClient";

export const dynamic = "force-dynamic";

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

export default async function PlanPage() {
  const { isAdmin, currentUser } = await getPlanData();
  return <PlanClient isAdmin={isAdmin} currentUser={currentUser} />;
}
