<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://yourwebsite.com
 * @since      1.0.0
 * @package    Surftrust
 * @subpackage Surftrust/admin
 */
class Surftrust_Admin
{

    private $plugin_name;
    private $version;

    public function __construct($plugin_name, $version)
    {
        $this->plugin_name = $plugin_name;
        $this->version = $version;
    }


    public function check_woocommerce_dependency()
    {
        // Make sure the is_plugin_active function is available
        if (! function_exists('is_plugin_active')) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        // Get the current screen to check if we are on a SurfPop page
        $screen = get_current_screen();
        // Only show the notice on our pages (the main SPA or the notification editor)
        if (! $screen || (strpos($screen->id, 'surftrust') === false && $screen->id !== 'st_notification')) {
            return;
        }

        // The core check: if WooCommerce is NOT active, display the error.
        if (! is_plugin_active('woocommerce/woocommerce.php')) {
?>
            <div class="notice notice-error is-dismissible" >
                <p style="color: black;">
                    <strong><?php esc_html_e('SurfPop Requires WooCommerce!', 'surfpop'); ?></strong>
                    <?php esc_html_e('The SurfPop plugin is designed to work with WooCommerce and it is currently inactive. Please activate WooCommerce for SurfPop to function correctly.', 'surfpop'); ?>
                    <a href="<?php echo esc_url(admin_url('plugins.php')); ?>" class="button button-primary" style="margin-left: 10px;"><?php esc_html_e('Activate WooCommerce', 'surfpop'); ?></a>
                </p>
            </div>
        <?php
        }
    }
    /**
     * Register the admin menu for the plugin.
     */

    // In /surftrust/admin/class-surftrust-admin.php

    // In class-surftrust-admin.php
    // In /admin/class-surftrust-admin.php


    public function add_admin_menu()
    {
        add_menu_page('SurfPop', 'SurfPop', 'manage_options', 'surftrust', array($this, 'display_main_app_page'), 'dashicons-bell', 30);

        // ALL SPA submenu items MUST use the parent slug 'surftrust'.
        // This tells WordPress they are all the SAME page.
        add_submenu_page('surftrust', 'Dashboard', 'Dashboard', 'manage_options', 'surftrust', array($this, 'display_main_app_page'));
        add_submenu_page('surftrust', 'All Notifications', 'All Notifications', 'manage_options', 'surftrust', array($this, 'display_main_app_page'));
        add_submenu_page('surftrust', 'Settings', 'Settings', 'manage_options', 'surftrust', array($this, 'display_main_app_page'));
        add_submenu_page('surftrust', 'Analytics', 'Analytics', 'manage_options', 'surftrust', array($this, 'display_main_app_page'));

        // The "Add New" link is external and remains unchanged. This is correct.
        add_submenu_page('surftrust', 'Add New', 'Add New', 'manage_options', 'post-new.php?post_type=st_notification');
    }

    /**
     * Render the basic HTML structure for the admin page.
     * The React app will mount onto the 'surftrust-admin-app' div.
     */
    /**
     * Renders the root div for the main Settings React application.
     */

    // Add this new function
    public function display_main_app_page()
    {
        // Note the new ID that our index.js file is looking for
        echo '<div class="wrap" id="surftrust-app-root"></div>';
    }

    // Delete the old display_settings_page(), display_analytics_page(), etc.
    public function display_settings_page()
    {
        echo '<div class="wrap"><div id="surftrust-settings-app"></div></div>';
    }

    /**
     * Renders the root div for the Analytics React application.
     */
    public function display_analytics_page()
    {
        echo '<div class="wrap"><div id="surftrust-analytics-app"></div></div>';
    }

    public function display_dashboard_page()
    {
        echo '<div class="wrap"><div id="surftrust-dashboard-app"></div></div>';
    }
    public function display_all_notifications_page()
    {
        echo '<div class="wrap"><div id="surftrust-all-notifications-app"></div></div>';
    }
    // In /admin/class-surftrust-admin.php




    // In /admin/class-surftrust-admin.php

    public function modify_submenu_urls()
    {
        global $submenu;
        $parent_slug = 'surftrust';

        if (!isset($submenu[$parent_slug])) {
            return;
        }

        // Map the unique MENU TITLE to our React Router paths.
        $title_to_hash_map = [
            'Dashboard'         => '#/dashboard',
            'All Notifications' => '#/all-notifications',
            'Settings'          => '#/settings',
            'Analytics'         => '#/analytics',
        ];

        foreach ($submenu[$parent_slug] as $key => $item) {
            $menu_title = $item[0];
            if (isset($title_to_hash_map[$menu_title])) {
                // Append the hash directly to the parent slug.
                // The final link becomes: admin.php?page=surftrust#/settings
                $submenu[$parent_slug][$key][2] = 'admin.php?page=' . $parent_slug . $title_to_hash_map[$menu_title];
            }
        }
    }




    // In /admin/class-surftrust-admin.php

    // In /admin/class-surftrust-admin.php

    // Rename the function to better reflect its new purpose
    public function create_builder_header($post)
    {


        // REPLACE your old create_builder_header function with this new one

        if ('st_notification' !== $post->post_type) {
            return;
        }

        $dashboard_url = admin_url('admin.php?page=surftrust#/dashboard');
        $add_new_url = admin_url('post-new.php?post_type=st_notification');
        $all_notifications_url = admin_url('admin.php?page=surftrust#/all-notifications'); // URL for the new button
        $is_new = get_current_screen()->action === 'add';
        $title = $is_new ? 'Create a New Notification' : 'Edit Notification';

        ?>
        <div class="st-builder-page-header">
            <div class="st-builder-page-logo-wrapper">
                <a href="<?php echo esc_url($dashboard_url); ?>" class="st-builder-logo-link">SurfPop</a>
            </div>



            <!-- THIS IS THE NEW SECTION FOR YOUR BUTTON -->
            <div class="st-builder-header-actions">
                <a href="<?php echo esc_url($add_new_url); ?>" class="button st-builder-add-new-button">
                    <span class="dashicons dashicons-plus"></span>
                    Create New
                </a>
                <a href="<?php echo esc_url($all_notifications_url); ?>" class="button st-builder-add-new-button">
                    <span class="dashicons dashicons-plus"></span>
                    All Notifications
                </a>
            </div>
            <!-- END OF NEW SECTION -->
        </div>
        <div class="st-builder-page-title-wrapper">

            <h1 class="st-builder-page-title"><?php echo esc_html($title); ?></h1>
        </div>
<?php

    }


public function enqueue_inline_admin_css() {
    $screen = get_current_screen();

    if (!is_object($screen) || $screen->id !== 'st_notification') {
        return;
    }

    // Create empty style handle to attach inline CSS
    wp_register_style('surftrust-inline-admin-css', false);
    wp_enqueue_style('surftrust-inline-admin-css');

    $css = '
        /* Hide native title & body content */
        #titlediv, #post-body-content {
            display: none;
        }

        /* Hide the default Add New button */
        body.post-type-st_notification .wrap .page-title-action {
            display: none !important;
        }
    ';

    wp_add_inline_style('surftrust-inline-admin-css', $css);
}

    // This is the closing brace of the class
    public function enqueue_scripts($hook_suffix)
    {
        $screen = get_current_screen();

        // We only need to check for the ONE top-level hook for our SPA.
        $is_our_spa_page = ($hook_suffix === 'toplevel_page_surftrust');
        $is_our_builder_page = (is_object($screen) && $screen->post_type === 'st_notification');

        // Load styles on ANY of our plugin's pages.
        if ($is_our_spa_page || $is_our_builder_page) {
            wp_enqueue_style('wp-components');
            wp_enqueue_style('surftrust-admin-styles', plugin_dir_url(__FILE__) . 'css/surftrust-admin.css', array(), $this->version);
        }

        // Load the MAIN SPA SCRIPT only when we are on our SPA's page.
        if ($is_our_spa_page) {
            $handle = 'surftrust-main-app';
            wp_enqueue_script($handle, plugin_dir_url(__FILE__) . '../build/app.js', ['wp-element', 'wp-api-fetch', 'wp-components'], filemtime(plugin_dir_path(__FILE__) . '../build/app.js'), true);
            wp_localize_script($handle, 'surftrust_admin_data', [
                'nonce' => wp_create_nonce('wp_rest'),
                'plugin_url' => plugin_dir_url(dirname(__FILE__)),
                'add_new_url' => admin_url('post-new.php?post_type=st_notification')
            ]);
        }

        // Load the BUILDER APP SCRIPT only on the CPT edit screen.
        if ($is_our_builder_page) {
            $handle = 'surftrust-builder-app';
            wp_enqueue_script($handle, plugin_dir_url(__FILE__) . '../build/builder.js', ['wp-element', 'wp-api-fetch', 'wp-components'], filemtime(plugin_dir_path(__FILE__) . '../build/builder.js'), true);
            global $post;
            $saved_settings = get_post_meta($post->ID, '_surftrust_settings', true);
            wp_localize_script($handle, 'surftrust_admin_data', ['nonce' => wp_create_nonce('wp_rest'), 'plugin_url' => plugin_dir_url(dirname(__FILE__)), 'settings' => is_array($saved_settings) ? $saved_settings : new stdClass(), 'all_notifications_url' => admin_url('admin.php?page=surftrust#/all-notifications')]);
        }
    }
};
