import { verifySession } from "@/lib/auth";
import { pgGetCustomerPricing, pgGetCustomerCategories, pgGetCustomers, pgGetMaterials } from "@/lib/postgres/data-access";
import { getTranslations, getCurrentLocale } from "@/lib/i18n";
import CustomerPricingForm from "@/components/admin/customer-pricing-form";
import type { Customer, CustomerCategory, CustomerPricing, ProductionItem } from "@/lib/types";

export default async function ManageCustomerPricingPage() {
  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 customers: Customer[] = (await pgGetCustomers({ page: 1, pageSize: 5000 })).items as Customer[];
  const materials: ProductionItem[] = (await pgGetMaterials({ page: 1, pageSize: 5000 })).items as ProductionItem[];
  const customerPricing: CustomerPricing[] = (await pgGetCustomerPricing({ page: 1, pageSize: 5000 })).items as CustomerPricing[];
  const categories: CustomerCategory[] = (await pgGetCustomerCategories({ page: 1, pageSize: 5000 })).items as CustomerCategory[];

  return (
    <CustomerPricingForm
      customers={customers}
      materials={materials}
      allPricing={customerPricing}
      categories={categories}
      t={tLists}
      tGlobal={tGlobal}
    />
  );
}
