<?php

namespace CPBW\Backend;

/**
 * The menu handler class
 */
class Menu {

    public function __construct() {
        add_action('admin_menu', [$this, 'cpbw_admin_menu']);
    }

    public function cpbw_admin_menu() {
        add_menu_page(
            __('Better Badge', 'custom-product-badge-for-woocommerce'),
            __('Better Badge', 'custom-product-badge-for-woocommerce'),
            'manage_options',
            'cpbw',
            [$this, 'plugin_page'],
            CPBW_ASSETS . '/image/cpbw-menu-img.png',
            66
        );

        add_submenu_page( 
			'cpbw', 
			__( 'Badges', 'custom-product-badge-for-woocommerce' ), 
			__( 'Badges', 'custom-product-badge-for-woocommerce' ), 
			'manage_options', 
			'cpbw', 
			[ $this, 'plugin_page'], 
		);

        add_submenu_page(
            'cpbw',
            __('Create Badge', 'custom-product-badge-for-woocommerce'),
            __('Create Badge', 'custom-product-badge-for-woocommerce'),
            'manage_options',
            'cpbw#/editor',
            [$this, 'plugin_page']
        );

        add_submenu_page(
            'cpbw',
            __('Filters', 'custom-product-badge-for-woocommerce'),
            __('Filters', 'custom-product-badge-for-woocommerce'),
            'manage_options',
            'cpbw#/filters',
            [$this, 'plugin_page']
        );

        add_submenu_page(
            'cpbw',
            __('Settings', 'custom-product-badge-for-woocommerce'),
            __('Settings', 'custom-product-badge-for-woocommerce'),
            'manage_options',
            'cpbw#/settings',
            [$this, 'plugin_page']
        );

        // Check if Pro version is NOT active
        if ( ! class_exists('CPBW_Pro_Main') ) {
            add_submenu_page(
                'cpbw',
                __('Get Pro', 'custom-product-badge-for-woocommerce'),
                __('Get Pro ⭐', 'custom-product-badge-for-woocommerce'),
                'manage_options',
                'cpbw-upgrade-pro',
                function() {
                    wp_redirect('https://innocodia.com/plugins/woocommerce-product-badges-pro/');
                    exit;
                }
            );

            // Add CSS + JS for styling and new tab behavior
            add_action('admin_head', function() {
                ?>
                    <script>
                        document.addEventListener('DOMContentLoaded', function() {
                            const proMenu = document.querySelector('#toplevel_page_cpbw ul.wp-submenu a[href$="cpbw-upgrade-pro"]');
                            if (proMenu) {
                                proMenu.setAttribute('target', '_blank');
                                proMenu.setAttribute('href', 'https://innocodia.com/plugins/woocommerce-product-badges-pro/');
                            }
                        });
                    </script>
                <?php
            });
        }
    }


    /**
     * Callback function to display the plugin page.
     */
    public function plugin_page() {
        // Load the view from the plugin directory.
        require_once CPBW_DIR_PATH . '/backend/views/admin-view.php';
    }
}