
import { verifySession } from "@/lib/auth";

import { getCurrentLocale, getTranslations } from "@/lib/i18n";
import PartyStatement from "@/components/admin/party-statement";
import { getCurrencySymbolAsync } from "@/lib/server-currency";
import type { SupplierPayment, Currency } from "@/lib/types";

export default async function SupplierStatementPage() {
  const session = await verifySession();
  const user = session.user;
  const locale = await getCurrentLocale();
  const t = getTranslations(locale);
  const tStatement = t.PartyStatement ?? {};
  const tGlobal = t.Global ?? {};

  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>
    );
  }

  let customers = [];
  let suppliers = [];
  let invoices = [];
  let orders = [];
  let returns = [];
  let payments = [];
  let currencies: Currency[] = [];
  let supplierPayments: SupplierPayment[] = [];

  
  const {
  pgGetCustomers,
  pgGetSuppliers,
  pgGetInvoices,
  pgGetPurchaseOrders,
  pgGetPurchaseReturns,
  pgGetPayments,
  pgGetSupplierPayments,
  pgGetCurrencies,
  pgGetSettings,
  } = await import('@/lib/postgres/data-access');

  const [
  customersResult,
  suppliersResult,
  invoicesResult,
  ordersResult,
  returnsResult,
  paymentsResult,
  supplierPaymentsResult,
  currenciesResult,
  settings,
  ] = await Promise.all([
  pgGetCustomers({ page: 1, pageSize: 5000 }),
  pgGetSuppliers({ page: 1, pageSize: 5000 }),
  pgGetInvoices({ page: 1, pageSize: 5000 }),
  pgGetPurchaseOrders({ page: 1, pageSize: 5000 }),
  pgGetPurchaseReturns({ page: 1, pageSize: 5000 }),
  pgGetPayments({ page: 1, pageSize: 5000 }),
  pgGetSupplierPayments({ page: 1, pageSize: 5000 }),
  pgGetCurrencies({ page: 1, pageSize: 500 }),
  pgGetSettings(),
  ]);

  customers = (customersResult.items || []) as typeof customers;
  suppliers = (suppliersResult.items || []) as typeof suppliers;
  invoices = (invoicesResult.items || []) as typeof invoices;
  orders = (ordersResult.items || []) as typeof orders;
  returns = (returnsResult.items || []) as typeof returns;
  payments = (paymentsResult.items || []) as typeof payments;
  currencies = (currenciesResult.items || []) as Currency[];
  supplierPayments = ((supplierPaymentsResult.items || []) as SupplierPayment[]).map((payment) => ({
  ...payment,
  status: payment.status === 'cancelled' ? 'cancelled' : 'active',
  }));
  

  const currencySymbol = await getCurrencySymbolAsync();

  return (
    <PartyStatement
      customers={customers}
      suppliers={suppliers}
      invoices={invoices}
      orders={orders}
      returns={returns}
      customerPayments={payments}
      supplierPayments={supplierPayments}
      t={tStatement}
      tGlobal={tGlobal}
      locale={locale}
      currencySymbol={currencySymbol}
      currencies={currencies}
      printingSettings={{
        printHeaderLeftText: String((settings as any)?.printHeaderLeftText || ''),
        printHeaderCenterText: String((settings as any)?.printHeaderCenterText || ''),
        printHeaderRightText: String((settings as any)?.printHeaderRightText || ''),
        printHeaderText: String((settings as any)?.printHeaderText || ''),
        printFooterText: String((settings as any)?.printFooterText || ''),
      }}
    />
  );
}
