import { verifySession } from "@/lib/auth";
import { pgGetItemGroups } from "@/lib/postgres/data-access";
import { getTranslations, getCurrentLocale } from "@/lib/i18n";
import ItemGroupsManager from "@/components/admin/item-groups-manager";
import type { ItemGroup } from "@/lib/types";

export default async function ManageItemGroupsPage() {
  const session = await verifySession();
  const user = session.user;
  const locale = await getCurrentLocale();
  const t = getTranslations(locale);
  const tGlobal = t.Global ?? {};
  const tLists = t.ItemLists ?? {};

  if (!user || user.role !== 'admin') {
    return (
      <div className="text-center">
        <h1 className="text-2xl font-bold">{tGlobal?.accessDenied ?? "الوصول مرفوض"}</h1>
        <p>{tGlobal?.noPermission ?? "ليس لديك صلاحية لعرض هذه الصفحة."}</p>
      </div>
    );
  }

  const groups: ItemGroup[] = (await pgGetItemGroups({ page: 1, pageSize: 5000 })).items as ItemGroup[];

  return (
    <div className="w-full max-w-4xl mx-auto">
      <ItemGroupsManager groups={groups} t={tLists} tGlobal={tGlobal} />
    </div>
  );
}
