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 ORDER_PRINT_CONFIG: DocumentPrintConfig = {
  documentType: 'sales_order',
  labelAr: 'أمر مبيعات',
  labelEn: 'Sales Order',
  icon: 'pi pi-shopping-cart',
  settingKeyPrefix: 'print.sales_order',

  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 Order',
      documentNumber: doc.order_number || '',
      date: doc.date || '',
      dueDate: doc.expected_delivery_date || 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: 'شاشة سامسونج 27 بوصة / Samsung 27" Monitor',
        code: 'PROD-020',
        description: '4K UHD، منفذ HDMI و DisplayPort',
        quantity: 3,
        unitName: 'قطعة',
        unitPrice: 1800,
        discount: 5,
        discountIsPercent: true,
        tax: 769.5,
        total: 5899.5,
      },
      {
        name: 'لوحة مفاتيح ميكانيكية / Mechanical Keyboard',
        code: 'PROD-021',
        description: '',
        quantity: 3,
        unitName: 'قطعة',
        unitPrice: 350,
        discount: 0,
        discountIsPercent: true,
        tax: 157.5,
        total: 1207.5,
      },
    ];

    return {
      documentTitle: 'أمر مبيعات',
      documentNumber: 'SO-2026-0031',
      date: '2026-02-20',
      dueDate: '2026-03-15',
      reference: 'QTN-2026-0015',
      partyLabel: 'العميل',
      partyName: 'مجموعة الريادة التجارية / Riyada Trading Group',
      partyAddress: 'حي العزيزية، الدمام',
      partyTaxNumber: '300112233445003',
      salesperson: 'خالد العتيبي',
      currency: 'SAR',
      items,
      subtotal: 6450,
      discountAmount: 270,
      taxAmount: 927,
      total: 7107,
      notes: 'التسليم خلال 15 يوم عمل',
      company: SAMPLE_COMPANY,
      dir: 'rtl',
      lang: 'ar',
      t: t as (key: string) => string,
    };
  },
};
