<?php
namespace CSB;

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

if (!class_exists('bchCodePlugin')) {
    class bchCodePlugin
    {
        public function __construct()
        {
            if (did_action('plugins_loaded')) {
                $this->bchPluginDependency();
            } else {
                add_action('plugins_loaded', [$this, 'bchPluginDependency']);
            }
            add_action('enqueue_block_assets', [$this, 'enqueueBlockAssets']);
            add_action('admin_enqueue_scripts', [$this, 'adminEnqueueScripts']);
            add_shortcode('csb_code', [$this, 'bch_shortcode_handler']);
        }

        public function bchPluginDependency()
        {
            require_once CSBCH_DIR_PATH . 'inc/function.php';
            if ( file_exists( CSBCH_DIR_PATH . 'inc/remote.php' ) ) {
                require_once CSBCH_DIR_PATH . 'inc/remote.php';
            }
            if ( file_exists( CSBCH_DIR_PATH . 'inc/ai.php' ) ) {
                require_once CSBCH_DIR_PATH . 'inc/ai.php';
            }
            require_once CSBCH_DIR_PATH . 'bch_block.php';
            require_once CSBCH_DIR_PATH . 'inc/class_bchAdmin.php';
        }
        public function enqueueBlockAssets()
        {
            // Handled inside OnInit in bch_block.php
        }
        public function adminEnqueueScripts($screen)
        {
            global $typenow;
           

            if ('bch_block' === $typenow) {
                wp_enqueue_script('shortcode-js', CSBCH_DIR_URL . '/build/shortcode.js', [], CSBCH_PLUGIN_VERSION, true);
                wp_enqueue_style('shortcode-css', CSBCH_DIR_URL . '/build/shortcode.css', [], CSBCH_PLUGIN_VERSION);
            }

            // Demo & Help dashboard React app. The submenu lives under the `bch_block` CPT, so it can be
            // reached as either edit.php?post_type=bch_block&page=bch_Dashboard or admin.php?page=bch_Dashboard.
            // Gate on the unique page slug so the assets load regardless of the entry URL (the previous
            // `$screen === 'bch_block_page_bch_Dashboard'` check failed for the admin.php form, leaving the page blank).
            // Use sanitize_text_field (not sanitize_key) to preserve the slug's uppercase `D`.
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen check, no state change.
            $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';

            if ('bch_Dashboard' === $page) {
                $assets = include CSBCH_DIR_PATH . 'build/admin-dashboard.asset.php';

                wp_enqueue_script('bch-admin-script', CSBCH_DIR_URL . 'build/admin-dashboard.js', array_merge($assets['dependencies'], ['wp-util']), CSBCH_PLUGIN_VERSION, true);

                wp_enqueue_style('bch-admin-style', CSBCH_DIR_URL . '/build/admin-dashboard.css', false, CSBCH_PLUGIN_VERSION);
            }

        }
        public function bch_shortcode_handler($atts)
        {
            if (empty($atts) || !isset($atts['id'])) {
                return '<p>Please provide a valid ID.</p>';
            }
            $post = get_post($atts['id']);

            if ($post) {
                $blocks = parse_blocks($post->post_content);
                $output = '';
                $is_premium = csbIsPremium();

                if (!empty($blocks)) {
                    foreach ($blocks as $block) {
                        if (!empty($block['blockName'])) {
                            // Skip the parent selector block
                            if ($block['blockName'] === 'bch/code-parent') {
                                continue;
                            }

                            // Enforce free vs pro restrictions
                            if (strpos($block['blockName'], 'bch/') === 0) {
                                if (!$is_premium && $block['blockName'] !== 'bch/code-highlight') {
                                    continue;
                                }
                            }
                            $output .= render_block($block);
                        } else {
                            $output .= render_block($block);
                        }
                    }
                }
                return $output;
            } else {
                return '<p>Error: Code Snippets Block with ID ' . esc_html($atts['id']) . ' not found.</p>';
            }

        }
    }

}