<?php
/**
 * Page AI Chat - Loader Class
 * 
 * Enqueues the AI chat sidebar scripts on post/page edit screens
 *
 * @package PufferGo_Core
 * @since 2.2.0
 */

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

class PufferGo_Page_AI_Chat {

    /**
     * Plugin version for cache busting
     */
    const VERSION = '1.0.0';

    /**
     * Constructor - Register hooks
     */
    public function __construct() {
        // Only load in admin
        if ( is_admin() ) {
            add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );
        }
    }

    /**
     * Enqueue assets for the block editor
     * This hook fires only on Gutenberg editor screens
     */
    public function enqueue_editor_assets() {
        // AI Page Editor sidebar removed from edit page UI (option no longer shown).
        return;

        puffergo_log( '🔍 [Page AI Chat] enqueue_editor_assets() called' );
        
        $screen = get_current_screen();
        puffergo_log( '🔍 [Page AI Chat] Current screen: ' . ( $screen ? $screen->base : 'NULL' ) );
        
        // Only load on post/page edit screens
        if ( ! $screen || ! in_array( $screen->base, array( 'post', 'post-new' ), true ) ) {
            puffergo_log( '⚠️ [Page AI Chat] NOT loading - screen check failed' );
            return;
        }
        
        puffergo_log( '✅ [Page AI Chat] Screen check passed, proceeding to enqueue...' );

        $plugin_url = plugin_dir_url( dirname( __FILE__ ) );
        $plugin_dir = plugin_dir_path( dirname( __FILE__ ) );
        $asset_file = $plugin_dir . 'build/admin/page-ai-chat/index.asset.php';

        // Get dependencies and version from asset file
        if ( file_exists( $asset_file ) ) {
            $asset = include $asset_file;
            $dependencies = $asset['dependencies'];
            $version = $asset['version'];
        } else {
            // Fallback dependencies
            $dependencies = array(
                'wp-plugins',
                'wp-edit-post',
                'wp-editor',
                'wp-element',
                'wp-components',
                'wp-data',
                'wp-api-fetch',
                'wp-i18n',
                'wp-notices',
            );
            $version = self::VERSION;
        }

        // Enqueue JavaScript
        $js_file = $plugin_dir . 'build/admin/page-ai-chat/index.js';
        puffergo_log( '🔍 [Page AI Chat] JS file path: ' . $js_file );
        puffergo_log( '🔍 [Page AI Chat] JS file exists: ' . ( file_exists( $js_file ) ? 'YES' : 'NO' ) );
        
        if ( file_exists( $js_file ) ) {
            wp_enqueue_script(
                'puffergo-page-ai-chat',
                $plugin_url . 'build/admin/page-ai-chat/index.js',
                $dependencies,
                $version,
                true
            );
            
            puffergo_log( '✅ [Page AI Chat] JavaScript enqueued successfully!' );
            puffergo_log( '🔍 [Page AI Chat] Script URL: ' . $plugin_url . 'build/admin/page-ai-chat/index.js' );
            puffergo_log( '🔍 [Page AI Chat] Dependencies: ' . wp_json_encode( $dependencies ) );

            // Localize script with config
            wp_localize_script( 'puffergo-page-ai-chat', 'puffergoPageAIChat', array(
                'restUrl' => rest_url( 'puffergo/v1/' ),
                'nonce'   => wp_create_nonce( 'wp_rest' ),
            ) );
            
            puffergo_log( '✅ [Page AI Chat] Script localized with config' );
        } else {
            puffergo_log( '❌ [Page AI Chat] JavaScript file NOT FOUND!' );
        }

        // Enqueue styles
        $css_file = $plugin_dir . 'build/admin/page-ai-chat/style-index.css';
        puffergo_log( '🔍 [Page AI Chat] CSS file exists: ' . ( file_exists( $css_file ) ? 'YES' : 'NO' ) );
        
        if ( file_exists( $css_file ) ) {
            wp_enqueue_style(
                'puffergo-page-ai-chat',
                $plugin_url . 'build/admin/page-ai-chat/style-index.css',
                array( 'wp-components' ),
                $version
            );
            puffergo_log( '✅ [Page AI Chat] CSS enqueued successfully!' );
        }
        
        puffergo_log( '🏁 [Page AI Chat] enqueue_editor_assets() completed' );
    }
}

// Initialize the class
new PufferGo_Page_AI_Chat();
