<?php
namespace Lean_CSNS;

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

/**
 * Centralized assets management for scripts and styles
 */
class Assets_Manager {
    
    /**
     * Singleton instance
     * 
     * @var Assets_Manager
     */
    private static $instance = null;
    
    /**
     * Get singleton instance
     * 
     * @return Assets_Manager
     */
    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * Private constructor to prevent direct instantiation
     */
    private function __construct() {
        $this->init();
    }
    
    /**
     * Initialize assets management
     */
    private function init() {
        add_action('wp_enqueue_scripts', [$this, 'register_assets']);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_assets']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);
    }
    
    /**
     * Register all plugin assets
     */
    public function register_assets() {
        // Frontend JavaScript
        wp_register_script(
            'lean-csns-frontend',
            LEAN_CSNS_PL_URL . 'assets/js/frontend.js',
            ['jquery'],
            lean_csns_get_version(),
            true
        );

        // Shared design tokens from lex-settings lib — normally admin-only, so we register it here for the frontend.
        wp_register_style(
            'lean-csns-lex-tokens',
            LEAN_CSNS_PL_URL . 'includes/libs/lex-settings-new/core/assets/css/lex-tokens.css',
            [],
            lean_csns_get_version()
        );

        // Frontend CSS — depends on lex-tokens so --lex-space-* and other primitives are available.
        wp_register_style(
            'lean-csns-frontend',
            LEAN_CSNS_PL_URL . 'assets/css/frontend.css',
            ['lean-csns-lex-tokens'],
            lean_csns_get_version()
        );
    }
    
    /**
     * Enqueue frontend assets when needed
     */
    public function enqueue_frontend_assets() {
        // Check if this is ANY type of cart page
        $is_on_cart_page = is_cart() || has_block('woocommerce/cart');

        // Only load on cart pages
        if (!$is_on_cart_page) {
            return;
        }
        
        wp_enqueue_script('lean-csns-frontend');
        wp_enqueue_style('lean-csns-frontend');
        
        // Get buttons HTML from single source of truth
        $buttons_html = WC_Integration::get_instance()->get_buttons_html();
        
        // Localize script with parameters for frontend
        wp_localize_script('lean-csns-frontend', 'lean_csns_params', [
            'ajax_url'        => admin_url('admin-ajax.php'),
            'nonce'           => wp_create_nonce('lean_csns_nonce'),
            'buttons_html'    => $buttons_html, // Pre-rendered button HTML
            'button_position' => lean_csns_get_option( 'cart_share.button_position', 'default' ),
            'i18n' => [
                'share_title' => __('Share Your Cart', 'lean-cart-share-and-save'),
                'save_title' => __('Save Your Cart', 'lean-cart-share-and-save'),
                'copy_button' => __('Copy', 'lean-cart-share-and-save'),
                'close_button' => __('Close', 'lean-cart-share-and-save'),
                'save_button' => __('Save', 'lean-cart-share-and-save'),
                'cancel_button' => __('Cancel', 'lean-cart-share-and-save'),
                'enter_cart_name' => __('Enter cart name...', 'lean-cart-share-and-save'),
                'cart_name_required' => __('Please enter a cart name', 'lean-cart-share-and-save'),
                'link_copied' => __('Link copied to clipboard!', 'lean-cart-share-and-save')
            ]
        ]);
    }
    
    /**
     * Enqueue admin assets when needed
     */
    public function enqueue_admin_assets($hook) {
        // Only load on our admin pages
        if ($hook !== 'toplevel_page_lean-csns-settings') {
            return;
        }

        wp_enqueue_style(
            'lean-csns-admin',
            LEAN_CSNS_PL_URL . 'assets/css/admin.css',
            [],
            lean_csns_get_version()
        );
    }
}

// Initialize singleton instance
Assets_Manager::get_instance();