<?php

declare(strict_types=1);
if (!defined('WPINC')) {
    die();
}
/**
 * Class CPEPAI_Assets_Service
 * Handles the registration and enqueuing of scripts and styles.
 */
class CPEPAI_Assets_Service
{
    /**
     * Register the service.
     *
     * @return void
     */
    public function register(): void
    {
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_assets']);
    }

    /**
     * Enqueue admin assets.
     *
     * @param string $hook The current admin page.
     * @return void
     */
    public function enqueue_admin_assets(string $hook): void
    {
        if ('toplevel_page_cplanteam-ai-content-planner' !== $hook) {
            return;
        }

        // Get the asset file with version hash
        $asset_file = CPEPAI_PLUGIN_DIR . 'assets/build/js/app.asset.php';
        $asset = file_exists($asset_file)
            ? require $asset_file
            : ['dependencies' => ['wp-element', 'wp-i18n', 'react', 'react-dom'], 'version' => CPEPAI_VERSION];

        // Enqueue the main JavaScript file
        wp_enqueue_script(
            'cpepai-app',
            CPEPAI_PLUGIN_URL . 'assets/build/js/app.js',
            $asset['dependencies'],
            $asset['version'],
            true
        );

        // Enqueue CSS file if it exists
        $css_file = CPEPAI_PLUGIN_DIR . 'assets/build/app.css';
        if (file_exists($css_file)) {
            wp_enqueue_style('cpepai-styles', CPEPAI_PLUGIN_URL . 'assets/build/app.css', [], $asset['version']);
        }

        // Localize script with WordPress REST API data
        wp_localize_script('cpepai-app', 'cpepai_data', [
            'apiUrl' => rest_url('cpepai/v1'),
            'nonce' => wp_create_nonce('wp_rest'),
            'pluginUrl' => CPEPAI_PLUGIN_URL,
            'pluginDir' => CPEPAI_PLUGIN_DIR,
            'version' => CPEPAI_VERSION,
            'locale' => function_exists('get_user_locale') ? get_user_locale() : get_locale()
        ]);
    }

    /**
     * Enqueue frontend assets.
     *
     * @return void
     */
    public function enqueue_frontend_assets(): void
    {
        // Only enqueue frontend assets if needed
        // This can be customized based on your plugin's frontend requirements

        // Example: Enqueue assets only on specific pages or when shortcodes are used
        // if (is_singular() && has_shortcode(get_post()->post_content, 'content_plan')) {
        //     $this->enqueue_plugin_assets();
        // }
    }

    /**
     * Enqueue common plugin assets.
     *
     * @return void
     */
    private function enqueue_plugin_assets(): void
    {
        // Get the asset file with version hash
        $asset_file = CPEPAI_PLUGIN_DIR . 'assets/build/js/app.asset.php';
        $asset = file_exists($asset_file)
            ? require $asset_file
            : ['dependencies' => ['wp-element', 'wp-i18n', 'react', 'react-dom'], 'version' => CPEPAI_VERSION];

        // Enqueue the main JavaScript file
        wp_enqueue_script(
            'cpepai-app',
            CPEPAI_PLUGIN_URL . 'assets/build/js/app.js',
            $asset['dependencies'],
            $asset['version'],
            true
        );

        // Localize script with WordPress REST API data
        wp_localize_script('cpepai-app', 'cpepai_data', [
            'apiUrl' => rest_url('cpepai/v1'),
            'nonce' => wp_create_nonce('wp_rest'),
            'pluginUrl' => CPEPAI_PLUGIN_URL,
            'pluginDir' => CPEPAI_PLUGIN_DIR,
            'version' => CPEPAI_VERSION,
            'locale' => function_exists('get_user_locale') ? get_user_locale() : get_locale()
        ]);
    }
}
