import { verifySession } from "@/lib/auth";
import { pgGetProductShapeDefinitions } from "@/lib/postgres/data-access";
import { getTranslations, getCurrentLocale } from "@/lib/i18n";
import { getCurrencySymbolAsync } from "@/lib/server-currency";
import ListManager from "@/components/admin/list-manager";
import type { ProductionItem } from "@/lib/types";

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

  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 productShapes: ProductionItem[] = (await pgGetProductShapeDefinitions({ page: 1, pageSize: 5000 })).items as ProductionItem[];

  return (
    <ListManager
      items={productShapes}
      title={tLists?.manageProductShapes ?? "إدارة الاحجام و الاشكال"}
      description={tLists?.manageProductShapesDescription ?? "إضافة وتعديل وحذف أشكال وأحجام المنتجات لاستخدامها كمرجع موحد."}
      itemType="product-shape"
      t={tLists}
      tGlobal={tGlobal}
      currencySymbol={currencySymbol}
    />
  );
}
