import { verifySession } from "@/lib/auth";
import { pgGetSuppliers } from "@/lib/postgres/data-access";
import { getTranslations, getCurrentLocale } from "@/lib/i18n";
import ListManager from "@/components/admin/list-manager";
import { getCurrencySymbolAsync } from "@/lib/server-currency";
import type { Supplier } from "@/lib/types";

export default async function ManageSuppliersPage() {
  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 suppliers: Supplier[] = (await pgGetSuppliers({ page: 1, pageSize: 5000 })).items as Supplier[];

  return (
    <ListManager
      items={suppliers}
      title={tLists?.manageSuppliers ?? "إدارة الموردين"}
      description={tLists?.manageSuppliersDescription ?? "إضافة وتعديل وحذف الموردين."}
      itemType="supplier"
      t={tLists}
      tGlobal={tGlobal}
      currencySymbol={currencySymbol}
    />
  );
}
