<?php
/**
 * Twig Template Loader for WC Tier Package Prices
 *
 * @package WC_Tier_Package_Prices
 */

if (!defined('ABSPATH')) {
    exit;
}

use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;

class WC_TPP_Template_Loader {

    private static $instance = null;
    private $twig;

    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        $this->init_twig();
    }

    private function init_twig() {
        $loader = new FilesystemLoader(WC_TPP_PLUGIN_DIR . 'templates');

        $this->twig = new Environment($loader, array(
            'cache' => WP_DEBUG ? false : WC_TPP_PLUGIN_DIR . 'templates/cache',
            'auto_reload' => true,
            'autoescape' => 'html',
        ));

        // Add WordPress translation filter
        $this->twig->addFilter(new TwigFilter('__', function ($string, $domain = 'wc-tier-package-prices') {
            return __($string, $domain);
        }));

        $this->twig->addFilter(new TwigFilter('_e', function ($string, $domain = 'wc-tier-package-prices') {
            _e($string, $domain);
        }));

        $this->twig->addFilter(new TwigFilter('esc_html', 'esc_html'));
        $this->twig->addFilter(new TwigFilter('esc_attr', 'esc_attr'));
        $this->twig->addFilter(new TwigFilter('esc_url', 'esc_url'));

        // Add WordPress functions
        $this->twig->addFunction(new TwigFunction('get_option', 'get_option'));
        $this->twig->addFunction(new TwigFunction('checked', function($checked, $current = true, $echo = false) {
            return checked($checked, $current, $echo);
        }));
        $this->twig->addFunction(new TwigFunction('selected', function($selected, $current = true, $echo = false) {
            return selected($selected, $current, $echo);
        }));
        $this->twig->addFunction(new TwigFunction('settings_fields', function($option_group) {
            settings_fields($option_group);
        }));
        $this->twig->addFunction(new TwigFunction('submit_button', function($text = null) {
            submit_button($text);
        }));
        $this->twig->addFunction(new TwigFunction('get_admin_page_title', 'get_admin_page_title'));
        $this->twig->addFunction(new TwigFunction('wc_price', 'wc_price'));
    }

    /**
     * Render a Twig template
     *
     * @param string $template Template file name (e.g., 'admin/settings-page.twig')
     * @param array $context Variables to pass to the template
     * @return string Rendered template
     */
    public function render($template, $context = array()) {
        try {
            return $this->twig->render($template, $context);
        } catch (Exception $e) {
            if (WP_DEBUG) {
                return sprintf(
                    '<div class="error"><p>Twig Error: %s</p></div>',
                    esc_html($e->getMessage())
                );
            }
            return '';
        }
    }

    /**
     * Display a Twig template
     *
     * @param string $template Template file name
     * @param array $context Variables to pass to the template
     */
    public function display($template, $context = array()) {
        echo $this->render($template, $context);
    }
}
