<?php
/**
 * PDF Helper Class
 *
 * @package Easy_Invoice
 * @subpackage Helpers
 */

namespace EasyInvoice\Includes\Helpers;

use EasyInvoice\Models\Invoice;

/**
 * PDF Helper Class
 *
 * Handles PDF generation for invoices with styling consistent with the web view
 */
class PdfHelper {

    /**
         * Get invoice data for PDF generation
         * @param \EasyInvoice\Includes\Models\Invoice $invoice
         * @return array
         */
        public static function getInvoiceDataForPdf($invoice) {
            $company = [
                'name' => get_bloginfo('name'),
                'address' => get_option('easy_invoice_company_address'),
                'email' => get_option('easy_invoice_company_email'),
                'phone' => get_option('easy_invoice_company_phone')
            ];

            $customer = [
                'name' => $invoice->getCustomerName(),
                'address' => $invoice->getCustomerAddress(),
                'email' => $invoice->getCustomerEmail()
            ];

            $items = [];
            foreach ($invoice->getItems() as $item) {
                $items[] = [
                    'name' => $item->getName(),
                    'description' => $item->getDescription(),
                    'quantity' => $item->getQuantity(),
                    'price' => $item->getPrice(),
                    'adjust' => $item->getAdjust(),
                    'total' => $item->getAmount()
                ];
            }

            $totals = [
                'subtotal' => $invoice->getSubtotal(),
                'tax' => $invoice->getTaxAmount(),
                'discount' => $invoice->getDiscountAmount(),
                'total' => $invoice->getTotal()
            ];

            return [
                'number' => $invoice->getNumber(),
                'date' => $invoice->getIssueDate(),
                'dueDate' => $invoice->getDueDate(),
                'status' => $invoice->getStatus(),
                'company' => $company,
                'customer' => $customer,
                'items' => $items,
                'totals' => $totals,
                'notes' => $invoice->getNotes(),
                'currency_code' => $invoice->getCurrencyCode() ?: 'USD',
                'currency_symbol' => \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol($invoice->getCurrencyCode() ?: 'USD'),
                'symbol_position' => get_option('easy_invoice_currency_symbol_position', 'before'),
                'template' => $invoice->getTemplate() ?: 'standard'
            ];
            }

        /**
         * Enqueue required scripts for PDF generation
     */
        public static function enqueuePdfScripts() {
            // Enqueue jsPDF
            wp_enqueue_script(
                'jspdf',
                'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js',
                [],
                '2.5.1',
                true
            );
        }
    }
