import {
  DocumentPrintConfig,
  GenericPrintData,
  GenericPrintItem,
} 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 QUOTATION_PRINT_CONFIG: DocumentPrintConfig = {
  documentType: 'sales_quotation',
  labelAr: 'عرض سعر',
  labelEn: 'Sales Quotation',
  icon: 'pi pi-file',
  settingKeyPrefix: 'print.sales_quotation',

  toGenericData(
    doc: any,
    company: any,
    dir: string,
    lang: string,
    t: Function
  ): GenericPrintData {
    const items: GenericPrintItem[] = (doc.items || []).map((item: any) => ({
      name: item.product_name || '',
      code: item.product_code || '',
      description: item.description || '',
      quantity: Number(item.quantity) || 0,
      unitName: item.unit_name || '',
      unitPrice: Number(item.unit_price) || 0,
      discount: Number(item.discount_percent) || 0,
      discountIsPercent: true,
      tax: Number(item.tax_amount) || 0,
      total: Number(item.line_total) || 0,
    }));

    return {
      documentTitle: lang === 'ar' ? 'عرض سعر' : 'Sales Quotation',
      documentNumber: doc.quotation_number || '',
      date: doc.date || '',
      dueDate: doc.valid_until || undefined,
      reference: doc.reference || undefined,
      partyLabel: lang === 'ar' ? 'العميل' : 'Customer',
      partyName: doc.customer_name || '',
      partyAddress: doc.customer_address || undefined,
      partyTaxNumber: doc.customer_tax_number || undefined,
      salesperson: doc.salesperson_name || undefined,
      currency: doc.currency_code || 'SAR',
      items,
      subtotal: Number(doc.subtotal) || 0,
      discountAmount: Number(doc.discount_amount) || 0,
      taxAmount: Number(doc.tax_amount) || 0,
      total: Number(doc.total) || 0,
      notes: doc.notes || undefined,
      company,
      dir: dir as 'rtl' | 'ltr',
      lang: lang as 'ar' | 'en',
      t: t as (key: string) => string,
    };
  },

  sampleData(t: Function): GenericPrintData {
    const items: GenericPrintItem[] = [
      {
        name: 'خادم HP ProLiant DL380 / HP ProLiant DL380 Server',
        code: 'PROD-010',
        description: 'خادم عالي الأداء مع ضمان 3 سنوات',
        quantity: 1,
        unitName: 'قطعة',
        unitPrice: 25000,
        discount: 10,
        discountIsPercent: true,
        tax: 3375,
        total: 25875,
      },
      {
        name: 'كابل شبكة Cat6 (50 متر) / Cat6 Network Cable (50m)',
        code: 'PROD-011',
        description: '',
        quantity: 10,
        unitName: 'لفة',
        unitPrice: 85,
        discount: 0,
        discountIsPercent: true,
        tax: 127.5,
        total: 977.5,
      },
    ];

    return {
      documentTitle: 'عرض سعر',
      documentNumber: 'QTN-2026-0018',
      date: '2026-02-25',
      dueDate: '2026-03-25',
      reference: 'RFQ-4421',
      partyLabel: 'العميل',
      partyName: 'شركة الأفق للحلول الرقمية / Horizon Digital Solutions Co.',
      partyAddress: 'طريق الأمير سلطان، جدة',
      partyTaxNumber: '300654321987003',
      salesperson: 'سارة أحمد',
      currency: 'SAR',
      items,
      subtotal: 25850,
      discountAmount: 2500,
      taxAmount: 3502.5,
      total: 26852.5,
      notes: 'العرض ساري لمدة 30 يوم من تاريخه',
      company: SAMPLE_COMPANY,
      dir: 'rtl',
      lang: 'ar',
      t: t as (key: string) => string,
    };
  },
};
