import { verifySession } from '@/lib/auth';

import { pgGetCustomerCategories } from '@/lib/postgres/data-access';
import { getCurrentLocale, getTranslations } from '@/lib/i18n';
import CustomerCategoriesManager from '@/components/admin/customer-categories-manager';
import type { CustomerCategory } from '@/lib/types';

export default async function ManageCategoriesPage() {
  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 ?? 'Access Denied'}</h1>
        <p>{tGlobal?.noPermission ?? 'You do not have permission to view this page.'}</p>
      </div>
    );
  }

  const categories: CustomerCategory[] = (await pgGetCustomerCategories({ page: 1, pageSize: 5000 })).items as CustomerCategory[];

  return <CustomerCategoriesManager categories={categories} t={tLists} tGlobal={tGlobal} />;
}
