import { cookies } from "next/headers";
import { verifyToken } from "@/lib/auth/jwt";
import { getAllPackages } from "@/lib/db/repos/packagesRepo";
import PackagesClient from "./PackagesClient";

export const dynamic = "force-dynamic";

async function getPackages() {
  try {
    const cookieStore = await cookies();
    const token = cookieStore.get("auth_token")?.value;
    if (!token) return [];
    const payload = await verifyToken(token);
    if (!payload || payload.role !== "admin") return [];
    return await getAllPackages();
  } catch {
    return [];
  }
}

export default async function PackagesPage() {
  const packages = await getPackages();
  return <PackagesClient initialPackages={packages} />;
}
