<?php
/**
 * Template Trait
 *
 * @package Easy_Invoice
 * @subpackage Traits
 */

namespace EasyInvoice\Traits;

use EasyInvoice\Controllers\SettingsController;

/**
 * TemplateTrait contains methods for template handling
 */
trait TemplateTrait {
    /**
     * Get common template variables
     * 
     * @return array Common template variables
     */
    protected function getCommonTemplateVars() {
        $settings_controller = new SettingsController();
        $settings = $settings_controller->getSettings();
        
        // Ensure settings is an array and has default values
        if (!is_array($settings)) {
            $settings = [];
        }
        
        return [
            'company' => $settings['company'] ?? '',
            'currency_symbol' => isset($settings['currency']['symbol']) ? $settings['currency']['symbol'] : '$',
            'currency_position' => isset($settings['currency']['position']) ? $settings['currency']['position'] : 'left',
        ];
    }
    
    /**
     * Display a template with given variables
     *
     * @param string $template_path Path to the template file
     * @param array $vars Variables to extract for the template
     * @return void
     */
    protected function displayTemplate($template_path, $vars = []) {
        // Add common template variables if not already provided
        if (!isset($vars['template_vars'])) {
            $vars['template_vars'] = $this->getCommonTemplateVars();
        }

        // Extract variables for use in the template
        extract($vars);
        
        // Include the template file
        if (file_exists($template_path)) {
            include $template_path;
        } else {
            wp_die(sprintf(__('Template file not found: %s', 'easy-invoice'), $template_path));
        }
    }
} 