<?php
/**
 * Template Text Helper
 * 
 * Centralized helper for retrieving text settings used in invoice and quote templates.
 * This prevents duplication and makes text management easier.
 * 
 * @package EasyInvoice
 * @since 2.0.0
 */

namespace EasyInvoice\Helpers;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class TemplateTextHelper {
    
    /**
     * Get all text settings for templates
     * 
     * @return array Array of text settings
     */
    public static function getAllTextSettings(): array {
        return [
            // Invoice specific
            'invoice_number' => \EasyInvoice\Controllers\SettingsController::getTextInvoiceNumber(),
            'invoice_date' => \EasyInvoice\Controllers\SettingsController::getTextInvoiceDate(),
            'due_date' => \EasyInvoice\Controllers\SettingsController::getTextDueDate(),
            'total_due' => \EasyInvoice\Controllers\SettingsController::getTextTotalDue(),
            
            // Quote specific
            'quote_number' => \EasyInvoice\Controllers\SettingsController::getTextQuoteNumber(),
            'quote_date' => \EasyInvoice\Controllers\SettingsController::getTextQuoteDate(),
            'valid_until' => \EasyInvoice\Controllers\SettingsController::getTextValidUntil(),
            
            // Common labels
            'to' => \EasyInvoice\Controllers\SettingsController::getTextTo(),
            'service' => \EasyInvoice\Controllers\SettingsController::getTextService(),
            'qty' => \EasyInvoice\Controllers\SettingsController::getTextQty(),
            'rate_price' => \EasyInvoice\Controllers\SettingsController::getTextRatePrice(),
            'adjust' => \EasyInvoice\Controllers\SettingsController::getTextAdjust(),
            'sub_total' => \EasyInvoice\Controllers\SettingsController::getTextSubTotal(),
            'total' => \EasyInvoice\Controllers\SettingsController::getTextTotal(),
            'tax' => \EasyInvoice\Controllers\SettingsController::getTextTax(),
            'discount' => \EasyInvoice\Controllers\SettingsController::getTextDiscount(),
            
            // Button labels
            'print' => \EasyInvoice\Controllers\SettingsController::getTextPrint(),
            'download_pdf' => \EasyInvoice\Controllers\SettingsController::getTextDownloadPdf(),
            'send_email' => \EasyInvoice\Controllers\SettingsController::getTextSendEmail(),
            'pay_now' => \EasyInvoice\Controllers\SettingsController::getTextPayNow(),
            'accept_quote' => \EasyInvoice\Controllers\SettingsController::getTextAcceptQuote(),
            'decline_quote' => \EasyInvoice\Controllers\SettingsController::getTextDeclineQuote(),
            'decline_reason' => \EasyInvoice\Controllers\SettingsController::getTextDeclineReason(),
        ];
    }
    
    /**
     * Get company information from settings
     * 
     * @return array Company information
     */
    public static function getCompanyInfo(): array {
        return [
            'name' => get_option('easy_invoice_company_name', 'Your Company Name'),
            'address' => get_option('easy_invoice_company_address', ''),
            'email' => get_option('easy_invoice_company_email', ''),
            'phone' => get_option('easy_invoice_company_phone', ''),
            'website' => get_option('easy_invoice_company_website', ''),
            'logo' => get_option('easy_invoice_company_logo', ''),
            'tax_number' => get_option('easy_invoice_tax_number', ''),
        ];
    }
    
    /**
     * Get invoice specific text settings
     * 
     * @return array Invoice text settings
     */
    public static function getInvoiceTextSettings(): array {
        return [
            'invoice_number' => \EasyInvoice\Controllers\SettingsController::getTextInvoiceNumber(),
            'invoice_date' => \EasyInvoice\Controllers\SettingsController::getTextInvoiceDate(),
            'due_date' => \EasyInvoice\Controllers\SettingsController::getTextDueDate(),
            'total_due' => \EasyInvoice\Controllers\SettingsController::getTextTotalDue(),
            'to' => \EasyInvoice\Controllers\SettingsController::getTextTo(),
            'service' => \EasyInvoice\Controllers\SettingsController::getTextService(),
            'qty' => \EasyInvoice\Controllers\SettingsController::getTextQty(),
            'rate_price' => \EasyInvoice\Controllers\SettingsController::getTextRatePrice(),
            'adjust' => \EasyInvoice\Controllers\SettingsController::getTextAdjust(),
            'sub_total' => \EasyInvoice\Controllers\SettingsController::getTextSubTotal(),
            'total' => \EasyInvoice\Controllers\SettingsController::getTextTotal(),
            'tax' => \EasyInvoice\Controllers\SettingsController::getTextTax(),
            'discount' => \EasyInvoice\Controllers\SettingsController::getTextDiscount(),
            'print' => \EasyInvoice\Controllers\SettingsController::getTextPrint(),
            'download_pdf' => \EasyInvoice\Controllers\SettingsController::getTextDownloadPdf(),
            'send_email' => \EasyInvoice\Controllers\SettingsController::getTextSendEmail(),
            'pay_now' => \EasyInvoice\Controllers\SettingsController::getTextPayNow(),
        ];
    }
    
    /**
     * Get quote specific text settings
     * 
     * @return array Quote text settings
     */
    public static function getQuoteTextSettings(): array {
        return [
            'quote_number' => \EasyInvoice\Controllers\SettingsController::getTextQuoteNumber(),
            'quote_date' => \EasyInvoice\Controllers\SettingsController::getTextQuoteDate(),
            'valid_until' => \EasyInvoice\Controllers\SettingsController::getTextValidUntil(),
            'to' => \EasyInvoice\Controllers\SettingsController::getTextTo(),
            'service' => \EasyInvoice\Controllers\SettingsController::getTextService(),
            'qty' => \EasyInvoice\Controllers\SettingsController::getTextQty(),
            'rate_price' => \EasyInvoice\Controllers\SettingsController::getTextRatePrice(),
            'adjust' => \EasyInvoice\Controllers\SettingsController::getTextAdjust(),
            'sub_total' => \EasyInvoice\Controllers\SettingsController::getTextSubTotal(),
            'total' => \EasyInvoice\Controllers\SettingsController::getTextTotal(),
            'tax' => \EasyInvoice\Controllers\SettingsController::getTextTax(),
            'discount' => \EasyInvoice\Controllers\SettingsController::getTextDiscount(),
            'print' => \EasyInvoice\Controllers\SettingsController::getTextPrint(),
            'download_pdf' => \EasyInvoice\Controllers\SettingsController::getTextDownloadPdf(),
            'send_email' => \EasyInvoice\Controllers\SettingsController::getTextSendEmail(),
            'accept_quote' => \EasyInvoice\Controllers\SettingsController::getTextAcceptQuote(),
            'decline_quote' => \EasyInvoice\Controllers\SettingsController::getTextDeclineQuote(),
            'decline_reason' => \EasyInvoice\Controllers\SettingsController::getTextDeclineReason(),
        ];
    }

    /**
     * Generate "From" section content for templates
     * 
     * @param array $company_info Company information array
     * @param bool $show_label Whether to show the "From" label
     * @param string $style Style variant for the "From" section
     * @return string Generated HTML for "From" section
     */
    public static function generateFromSection($company_info, $show_label = true, $style = 'default'): string {
        $output = '';
        
        switch ($style) {
            case 'minimal':
                // Minimal style - includes company name and details
                $output .= '<div class="company-info-minimal">';
                $output .= '<div class="company-name">' . esc_html($company_info['name']) . '</div>';
                if ($company_info['address']) {
                    $output .= '<div class="company-address">' . nl2br(esc_html($company_info['address'])) . '</div>';
                }
                if ($company_info['email']) {
                    $output .= '<div class="company-email">' . esc_html($company_info['email']) . '</div>';
                }
                if ($company_info['phone']) {
                    $output .= '<div class="company-phone">' . esc_html($company_info['phone']) . '</div>';
                }
                if ($company_info['website']) {
                    $output .= '<div class="company-website">' . esc_html($company_info['website']) . '</div>';
                }
                $output .= '</div>';
                break;
                
            case 'details-only':
                // Details only style - excludes company name (for use with separate company-name div)
                $output .= '<div class="company-info-details-only">';
                if ($company_info['address']) {
                    $output .= '<div class="company-address">' . nl2br(esc_html($company_info['address'])) . '</div>';
                }
                if ($company_info['email']) {
                    $output .= '<div class="company-email">' . esc_html($company_info['email']) . '</div>';
                }
                if ($company_info['phone']) {
                    $output .= '<div class="company-phone">' . esc_html($company_info['phone']) . '</div>';
                }
                if ($company_info['website']) {
                    $output .= '<div class="company-website">' . esc_html($company_info['website']) . '</div>';
                }
                $output .= '</div>';
                break;
                
            case 'elegant':
                // Elegant style - subtle design
                $output .= '<div class="company-info-elegant">';
                if ($show_label) {
                    $output .= '<h3 class="from-label">' . esc_html(\EasyInvoice\Controllers\SettingsController::getTextFrom()) . '</h3>';
                }
                $output .= '<div class="company-details">';
                $output .= '<div class="company-name">' . esc_html($company_info['name']) . '</div>';
                if ($company_info['address']) {
                    $output .= '<div class="company-address">' . nl2br(esc_html($company_info['address'])) . '</div>';
                }
                if ($company_info['email']) {
                    $output .= '<div class="company-email">' . esc_html($company_info['email']) . '</div>';
                }
                if ($company_info['phone']) {
                    $output .= '<div class="company-phone">' . esc_html($company_info['phone']) . '</div>';
                }
                if ($company_info['website']) {
                    $output .= '<div class="company-website">' . esc_html($company_info['website']) . '</div>';
                }
                $output .= '</div>';
                $output .= '</div>';
                break;
                
            case 'modern':
                // Modern style - clean and bold
                $output .= '<div class="company-info-modern">';
                if ($show_label) {
                    $output .= '<h2 class="from-label">' . esc_html(\EasyInvoice\Controllers\SettingsController::getTextFrom()) . ':</h2>';
                }
                $output .= '<div class="address-content">';
                $output .= '<div class="company-name">' . esc_html($company_info['name']) . '</div>';
                if ($company_info['address']) {
                    $output .= nl2br(esc_html($company_info['address'])) . '<br>';
                }
                if ($company_info['email']) {
                    $output .= esc_html($company_info['email']) . '<br>';
                }
                if ($company_info['phone']) {
                    $output .= esc_html($company_info['phone']) . '<br>';
                }
                if ($company_info['website']) {
                    $output .= esc_html($company_info['website']);
                }
                $output .= '</div>';
                $output .= '</div>';
                break;
                
            case 'professional':
                // Professional style - structured layout
                $output .= '<div class="company-info-professional">';
                if ($show_label) {
                    $output .= '<h2 class="from-label">' . esc_html(\EasyInvoice\Controllers\SettingsController::getTextFrom()) . ':</h2>';
                }
                $output .= '<div class="address-content">';
                $output .= '<div class="company-name">' . esc_html($company_info['name']) . '</div>';
                if ($company_info['address']) {
                    $output .= nl2br(esc_html($company_info['address'])) . '<br>';
                }
                if ($company_info['email']) {
                    $output .= esc_html($company_info['email']) . '<br>';
                }
                if ($company_info['phone']) {
                    $output .= esc_html($company_info['phone']) . '<br>';
                }
                if ($company_info['website']) {
                    $output .= esc_html($company_info['website']);
                }
                $output .= '</div>';
                $output .= '</div>';
                break;
                
            case 'creative':
                // Creative style - artistic design
                $output .= '<div class="company-info-creative">';
                if ($show_label) {
                    $output .= '<h2 class="from-label">' . esc_html(\EasyInvoice\Controllers\SettingsController::getTextFrom()) . '</h2>';
                }
                $output .= '<div class="address-content">';
                $output .= '<div class="company-name">' . esc_html($company_info['name']) . '</div>';
                if ($company_info['address']) {
                    $output .= nl2br(esc_html($company_info['address'])) . '<br>';
                }
                if ($company_info['email']) {
                    $output .= esc_html($company_info['email']) . '<br>';
                }
                if ($company_info['phone']) {
                    $output .= esc_html($company_info['phone']) . '<br>';
                }
                if ($company_info['website']) {
                    $output .= esc_html($company_info['website']);
                }
                $output .= '</div>';
                $output .= '</div>';
                break;
                
            default:
                // Default style - standard layout
                $output .= '<div class="company-info-default">';
                if ($show_label) {
                    $output .= '<h2 class="from-label">' . esc_html(\EasyInvoice\Controllers\SettingsController::getTextFrom()) . ':</h2>';
                }
                $output .= '<div class="address-content">';
                $output .= '<div class="company-name">' . esc_html($company_info['name']) . '</div>';
                if ($company_info['address']) {
                    $output .= nl2br(esc_html($company_info['address'])) . '<br>';
                }
                if ($company_info['email']) {
                    $output .= esc_html($company_info['email']) . '<br>';
                }
                if ($company_info['phone']) {
                    $output .= esc_html($company_info['phone']) . '<br>';
                }
                if ($company_info['website']) {
                    $output .= esc_html($company_info['website']);
                }
                $output .= '</div>';
                $output .= '</div>';
                break;
        }
        
        return $output;
    }

    /**
     * Generate totals section for invoice/quote templates
     * 
     * @param mixed $document Invoice or Quote object
     * @param array $text_settings Text settings array
     * @param mixed $formatter Formatter object
     * @param string $type 'invoice' or 'quote'
     * @return string Generated HTML for totals section
     */
    public static function generateTotalsSection($document, array $text_settings, $formatter, string $type = 'invoice'): string {
        $output = '';
        
        // Subtotal
        $output .= '<div class="' . $type . '-total-row">';
        $output .= '<span class="' . $type . '-total-label">' . esc_html($text_settings['sub_total']) . ':</span>';
        $output .= '<span class="' . $type . '-total-value">' . esc_html($formatter->format($document->getSubtotal())) . '</span>';
        $output .= '</div>';
        
        // Discount (if any)
        if ($document->getDiscountAmount() > 0) {
            $output .= '<div class="' . $type . '-total-row">';
            $output .= '<span class="' . $type . '-total-label">' . esc_html($text_settings['discount']);
            if ($document->getDiscountType() === 'percentage') {
                $output .= ' (' . esc_html($document->getDiscountValue()) . '%)';
            }
            $output .= ':</span>';
            $output .= '<span class="' . $type . '-total-value">-' . esc_html($formatter->format($document->getDiscountAmount())) . '</span>';
            $output .= '</div>';
        }
        
        
        // Tax (if any)
        if ($document->getTaxAmount() > 0) {
            $output .= '<div class="' . $type . '-total-row">';
            $output .= '<span class="' . $type . '-total-label">' . esc_html(easy_invoice_get_tax_name());
            $output .= ' (' . esc_html($document->getTaxRate()) . '%):</span>';
            $output .= '<span class="' . $type . '-total-value">' . esc_html($formatter->format($document->getTaxAmount())) . '</span>';
            $output .= '</div>';
        }
        
        // Allow plugins to add content
        if ($type === 'invoice') {
            do_action('easy_invoice_invoice_totals_after_tax', $document);
            do_action('easy_invoice_invoice_totals_after_discount', $document);
        } else {
            do_action('easy_invoice_quote_totals_after_tax', $document);
            do_action('easy_invoice_quote_totals_after_discount', $document);
        }
        
        // Grand Total
        $output .= '<div class="' . $type . '-total-row ' . $type . '-grand-total">';
        $output .= '<span class="' . $type . '-total-label">' . esc_html($text_settings['total']) . ':</span>';
        $output .= '<span class="' . $type . '-total-value">' . esc_html($formatter->format($document->getTotal())) . '</span>';
        $output .= '</div>';
        
        return $output;
    }
} 