<?php

/**
 * Cryzel Block Assets
 *
 * This file handles the registration and enqueuing of theme assets,
 * including styles and scripts for both the frontend and editor.
 *
 * @package Cryzel
 * @since 1.0.0
 */

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

if (! class_exists('Cryzel_Block_Assets')) {
    /**
     * Cryzel_Block_Assets Class
     * 
     * Manages the loading of theme assets for blocks and editor.
     */
    class Cryzel_Block_Assets
    {

        /**
         * Constructor
         * 
         * Sets up the hook for enqueuing block assets.
         */
        public function __construct()
        {
            add_action('enqueue_block_assets', [$this, 'enqueue_block_assets']);
        }

        /**
         * Enqueue block assets
         * 
         * Registers and enqueues styles and scripts for blocks.
         * Handles both frontend and editor assets.
         */
        public function enqueue_block_assets()
        {
            // Get theme version for cache busting
            $theme_version = wp_get_theme()->get('Version');

            // Register and enqueue styles
            wp_register_style(
                'cryzel-block-styles',
                get_template_directory_uri() . '/build/style-index.css',
                [],
                $theme_version
            );
            wp_enqueue_style('cryzel-block-styles');

            // Enqueue Dashicons for block icons (needed for Interactive Hotspot Image block)
            wp_enqueue_style('dashicons');

            // Load frontend scripts for all pages (including non-admin)
            $asset_path = get_template_directory() . '/build/index.asset.php';

            if (file_exists($asset_path)) {
                $asset_data = require $asset_path;

                // Register frontend script
                wp_register_script(
                    'cryzel-frontend-script',
                    get_template_directory_uri() . '/build/index.js',
                    $asset_data['dependencies'],
                    $asset_data['version'],
                    true
                );

                // Enqueue frontend script
                wp_enqueue_script('cryzel-frontend-script');
                
                // Add debug info for frontend
                if (!is_admin()) {
                    wp_localize_script('cryzel-frontend-script', 'cryzelDebug', array(
                        'script_loaded' => true,
                        'script_url' => get_template_directory_uri() . '/build/index.js',
                        'version' => $asset_data['version']
                    ));
                }
            }

            // Load editor scripts only in admin
            if (! is_admin()) {
                return;
            }

            if (file_exists($asset_path)) {
                $asset_data = require $asset_path;

                // Register helper script
                wp_register_script(
                    'cryzel-helper-script',
                    get_template_directory_uri() . '/assets/js/helper.js',
                    [],
                    $theme_version,
                    true
                );

                // Register main editor script
                wp_register_script(
                    'cryzel-block-editor-script',
                    get_template_directory_uri() . '/build/index.js',
                    $asset_data['dependencies'],
                    $asset_data['version'],
                    true
                );

                // Enqueue scripts
                wp_enqueue_script('cryzel-helper-script'); 
                wp_enqueue_script('cryzel-block-editor-script');

                wp_localize_script('cryzel-block-editor-script', 'cryzelParams', array(
                    'zolo_pro_status' => get_option('zolo_pro_status') ? 'active' : 'inactive',
                    'zoloblocks_installer' => array(
                        'ajax_url' => admin_url('admin-ajax.php'),
                        'nonce' => wp_create_nonce('cryzel_zoloblocks_nonce'),
                        'strings' => array(
                            'installing' => __('Installing ZoloBlocks plugin...', 'cryzel'),
                            'activating' => __('Activating ZoloBlocks plugin...', 'cryzel'),
                            'success' => __('ZoloBlocks plugin is ready!', 'cryzel'),
                            'reloading' => __('Reloading page to initialize ZoloBlocks plugin...', 'cryzel'),
                            'error' => __('Error: ', 'cryzel'),
                            'permission_error' => __('You do not have permission to install plugins.', 'cryzel'),
                            'plugin_required' => __('ZoloBlocks plugin is required to use the template library.', 'cryzel'),
                            'plugin_not_installed' => __('ZoloBlocks plugin is not installed. Would you like to install it now?', 'cryzel'),
                            'plugin_not_activated' => __('ZoloBlocks plugin is installed but not activated. Would you like to activate it now?', 'cryzel'),
                            'install_activate' => __('Install & Activate', 'cryzel'),
                            'activate' => __('Activate', 'cryzel'),
                            'cancel' => __('Cancel', 'cryzel')
                        )
                    )
                ));
            }
        }
    }

    // Initialize the class
    new Cryzel_Block_Assets();
}
