import { getTranslations, getCurrentLocale } from "@/lib/i18n";

import { SupplierPaymentVoucherForm } from "@/components/admin/supplier-payment-voucher-form";
import { getCurrencySymbolAsync } from "@/lib/server-currency";
import type { SupplierPayment } from "@/lib/types";

export default async function PaymentVoucherPage({
  searchParams,
}: {
  searchParams?: Promise<{ editId?: string }>;
}) {
  const resolvedSearchParams = await searchParams;
  const initialEditPaymentId = resolvedSearchParams?.editId;
  const locale = await getCurrentLocale();
  const t = getTranslations(locale);
  const tPurchases = t.Purchases ?? {};
  const tGlobal = t.Global ?? {};

  let suppliers = [];
  let customers = [];
  let purchaseOrders = [];
  let supplierPayments: SupplierPayment[] = [];
  let incomingChecks = [];
  let banks = [];
  let currencies = [];
  let defaultCurrency = undefined;

  
  const {
  pgGetSuppliers,
  pgGetCustomers,
  pgGetPurchaseOrders,
  pgGetSupplierPayments,
  pgGetBanks,
  pgGetChecksReceived,
  pgGetCurrencies,
  pgGetDefaultCurrency,
  } = await import('@/lib/postgres/data-access');

  const [
  suppliersResult,
  customersResult,
  purchaseOrdersResult,
  supplierPaymentsResult,
  banksResult,
  checksReceivedResult,
  currenciesResult,
  defaultCurrencyResult,
  ] = await Promise.all([
  pgGetSuppliers({ page: 1, pageSize: 5000 }),
  pgGetCustomers({ page: 1, pageSize: 5000 }),
  pgGetPurchaseOrders({ page: 1, pageSize: 5000 }),
  pgGetSupplierPayments({ page: 1, pageSize: 5000 }),
  pgGetBanks({ page: 1, pageSize: 5000 }),
  pgGetChecksReceived({ page: 1, pageSize: 5000 }),
  pgGetCurrencies({ page: 1, pageSize: 5000 }),
  pgGetDefaultCurrency(),
  ]);

  suppliers = (suppliersResult.items || []) as typeof suppliers;
  customers = (customersResult.items || []) as typeof customers;
  purchaseOrders = (purchaseOrdersResult.items || []) as typeof purchaseOrders;
  supplierPayments = ((supplierPaymentsResult.items || []) as SupplierPayment[]).map((payment) => ({
  ...payment,
  status: payment.status === 'cancelled' ? 'cancelled' : 'active',
  }));
  banks = (banksResult.items || []) as typeof banks;
  incomingChecks = (checksReceivedResult.items || []) as typeof incomingChecks;
  currencies = (currenciesResult.items || []) as typeof currencies;
  defaultCurrency = (defaultCurrencyResult as typeof defaultCurrency) || defaultCurrency;
  

  const currencySymbol = await getCurrencySymbolAsync();

  return (
    <div className="space-y-6 w-full">
      <div className="rounded-xl border bg-background p-3 sm:p-4">
        <SupplierPaymentVoucherForm
          suppliers={suppliers}
          customers={customers}
          purchaseOrders={purchaseOrders}
          supplierPayments={supplierPayments}
          incomingChecks={incomingChecks}
          banks={banks}
          currencies={currencies}
          defaultCurrency={defaultCurrency}
          initialEditPaymentId={initialEditPaymentId}
          t={tPurchases}
          tGlobal={tGlobal}
          currencySymbol={currencySymbol}
        />
      </div>
    </div>
  );
}
