import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { getTranslations, getCurrentLocale } from '@/lib/i18n';
import { PurchaseCreditNotesClient } from '../../../../components/admin/purchase-credit-notes-client';

import { getCurrencySymbolAsync } from '@/lib/server-currency';
import { formatCurrency as formatCurrencyUtil } from '@/lib/currency-formatter';

export default async function CreditNotesPage() {
  const locale = await getCurrentLocale();
  const t = getTranslations(locale);
  const tPurchases = t.Purchases ?? {};

  let creditNotes, invoices, suppliers;

  
  const { pgGetPurchaseCreditNotes, pgGetPurchaseOrders, pgGetSuppliers } = await import('@/lib/postgres/data-access');
  const [creditResult, invoiceResult, suppliersResult] = await Promise.all([
  pgGetPurchaseCreditNotes({ page: 1, pageSize: 5000 }),
  pgGetPurchaseOrders({ page: 1, pageSize: 5000 }),
  pgGetSuppliers({ page: 1, pageSize: 5000 })
  ]);
  creditNotes = creditResult.items || [];
  invoices = invoiceResult.items || [];
  suppliers = suppliersResult.items || [];
  

  const totalCreditNotes = creditNotes.length;
  const postedCreditNotes = creditNotes.filter(cn => cn.status === 'posted').length;
  const draftCreditNotes = creditNotes.filter(cn => cn.status === 'draft').length;
  const totalAmount = creditNotes
    .filter(cn => cn.status !== 'cancelled')
    .reduce((sum, cn) => sum + (cn.grandTotalBase || cn.grandTotal), 0);
  const currencySymbol = await getCurrencySymbolAsync();
  const formatAmount = (amount: number) => formatCurrencyUtil(amount, currencySymbol);

  return (
    <div className="space-y-6">
      <Card>
        <CardHeader>
          <CardTitle>{tPurchases.creditNotesTitle ?? 'إشعارات الدائن'}</CardTitle>
        </CardHeader>
        <CardContent className="space-y-4">
          <div className="grid grid-cols-1 md:grid-cols-4 gap-4">
            <div className="space-y-2 rounded-md border p-4">
              <div className="text-sm text-muted-foreground">{tPurchases.totalCreditNotes ?? 'إجمالي الإشعارات'}</div>
              <div className="text-2xl font-bold">{totalCreditNotes}</div>
            </div>
            <div className="space-y-2 rounded-md border p-4">
              <div className="text-sm text-muted-foreground">{tPurchases.draftCreditNotes ?? 'مسودات'}</div>
              <div className="text-2xl font-bold">{draftCreditNotes}</div>
            </div>
            <div className="space-y-2 rounded-md border p-4">
              <div className="text-sm text-muted-foreground">{tPurchases.postedCreditNotes ?? 'مرحلة'}</div>
              <div className="text-2xl font-bold">{postedCreditNotes}</div>
            </div>
            <div className="space-y-2 rounded-md border p-4">
              <div className="text-sm text-muted-foreground">{tPurchases.totalCreditAmount ?? 'إجمالي مبلغ الإشعارات'}</div>
              <div className="text-2xl font-bold">{formatAmount(totalAmount)}</div>
            </div>
          </div>
        </CardContent>
      </Card>

      <PurchaseCreditNotesClient
        creditNotes={creditNotes}
        invoices={invoices}
        suppliers={suppliers}
        t={tPurchases}
        currencySymbol={currencySymbol}
      />
    </div>
  );
}
