import {
  DocumentPrintConfig,
  GenericPrintData,
} from '../../../shared/print/print.interfaces';

const SAMPLE_COMPANY = {
  name: 'شركة القمر للبرمجيات / Moon Software Co.',
  address: 'شارع الملك فهد، حي الورود',
  city: 'الرياض',
  phone: '+966 11 234 5678',
  email: 'info@moon-erp.com',
  tax_number: '300123456789003',
  logo: '',
};

export const SUPPLIER_STATEMENT_PRINT_CONFIG: DocumentPrintConfig = {
  documentType: 'supplier_statement',
  labelAr: 'كشف حساب مورد',
  labelEn: 'Supplier Statement',
  icon: 'pi pi-file-check',
  settingKeyPrefix: 'print.supplier_statement',

  toGenericData(
    doc: any,
    company: any,
    dir: string,
    lang: string,
    t: Function
  ): GenericPrintData {
    const closingBalance = parseFloat(doc.closing_balance) || 0;

    const extraFields: { label: string; value: string }[] = [];

    if (doc.opening_balance !== undefined && doc.opening_balance !== null) {
      extraFields.push({
        label: t('PRINT.OPENING_BALANCE'),
        value: String(doc.opening_balance),
      });
    }

    if (doc.total_debit !== undefined && doc.total_debit !== null) {
      extraFields.push({
        label: t('PRINT.TOTAL_DEBIT'),
        value: String(doc.total_debit),
      });
    }

    if (doc.total_credit !== undefined && doc.total_credit !== null) {
      extraFields.push({
        label: t('PRINT.TOTAL_CREDIT'),
        value: String(doc.total_credit),
      });
    }

    if (doc.closing_balance !== undefined && doc.closing_balance !== null) {
      extraFields.push({
        label: t('PRINT.CLOSING_BALANCE'),
        value: String(doc.closing_balance),
      });
    }

    return {
      documentTitle: lang === 'ar' ? 'كشف حساب مورد' : 'Supplier Statement',
      documentNumber: doc.partner_name || '',
      date: (doc.from_date || '') + ' - ' + (doc.to_date || ''),
      partyLabel: t('PRINT.SUPPLIER'),
      partyName: doc.partner_name || '',
      currency: doc.currency?.code || 'SAR',
      items: [],
      subtotal: closingBalance,
      discountAmount: 0,
      taxAmount: 0,
      total: closingBalance,
      company,
      dir: dir as 'rtl' | 'ltr',
      lang: lang as 'ar' | 'en',
      t: t as (key: string) => string,
      extraFields,
    };
  },

  sampleData(t: Function): GenericPrintData {
    return {
      documentTitle: 'كشف حساب مورد',
      documentNumber: 'شركة الوادي للتوريدات',
      date: '2026-01-01 - 2026-02-27',
      partyLabel: 'المورد',
      partyName: 'شركة الوادي للتوريدات / Al-Wadi Supplies Co.',
      currency: 'SAR',
      items: [],
      subtotal: 9800,
      discountAmount: 0,
      taxAmount: 0,
      total: 9800,
      company: SAMPLE_COMPANY,
      dir: 'rtl',
      lang: 'ar',
      t: t as (key: string) => string,
      extraFields: [
        { label: 'الرصيد الافتتاحي', value: '3200' },
        { label: 'إجمالي المدين', value: '8750' },
        { label: 'إجمالي الدائن', value: '2150' },
        { label: 'الرصيد الختامي', value: '9800' },
      ],
    };
  },
};
