<?php
namespace ARCHM\Admin;

if ( ! defined('ABSPATH') ) {
    // phpcs:ignore Universal.PHP.RequireExitDieParentheses.Missing
    exit;
}

class Menu {


    /**
     * Indicates whether the user has a professional (pro) account.
     *
     * @var bool
     */
    public $is_pro = false;

    /**
     * Initialize the class
     *
     * @author Siam Maruf
     * @since 1.0
     */
    public function __construct() {
        add_action('admin_menu', [ $this, 'archive_menu' ]);
    }

    public function is_pro_active() {
        $plugin_list      = ( get_option('active_plugins') );
        $archive_mater_pro = 'archive-master-pro/archve-master.php';
        if ( ! in_array($archive_mater_pro, $plugin_list, true) ) {
            return false;
        }
        return true;
    }

    /**
     * Register menu Page
     *
     * @return void
     * @author Siam Maruf
     */
    // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.voidFound
    public function archive_menu(): void {
        global $submenu;

        $capability = 'manage_options';
        $slug = 'master-archives';

        $hook = add_menu_page(
            __('ArchiveMaster', 'archive-master'),
            __('ArchiveMaster', 'archive-master'),
            $capability,
            $slug,
            [ $this, 'menu_page_template' ],
            ARCHM_PLUGIN_ASSETS . '/images/master-archive-logo.png'
        );

        if ( current_user_can($capability) ) {
            $submenu[ $slug ][] = [ __('Dashboard', 'archive-master'), $capability, 'admin.php?page=' . $slug . '#/' ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
            $submenu[ $slug ][] = [ __('Logs', 'archive-master'), $capability, 'admin.php?page=' . $slug . '#/logs' ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
            $submenu[ $slug ][] = [ __('Archived Orders', 'archive-master'), $capability, 'admin.php?page=' . $slug . '#/archived-order' ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
            $submenu[ $slug ][] = [ __('Help & Documentation', 'archive-master'), $capability, 'admin.php?page=' . $slug . '#/help-documentation' ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
            if ( ! $this->is_pro_active() ) {
                // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
                $submenu[ $slug ][] = [
					__('<div class="archm-upgrade-plan"><span class="archm-upgrade-icon-text">Upgrade Now</span><span class="archm-upgrade-icon"><svg width="16" height="18" viewBox="0 0 16 18" fill="none" xmlns="http://www.w3.org/2000/svg">
				<path d="M1.59613 7.90625H7.45314V17.75H4.06253C3.65525 17.7521 3.26172 17.6028 2.95848 17.3309C2.65525 17.059 2.46399 16.684 2.42191 16.2789L1.59613 7.90625Z" fill="#34D399"/>
				<path d="M14.4039 7.90625L13.5672 16.2789C13.5251 16.684 13.3338 17.059 13.0306 17.3309C12.7274 17.6028 12.3338 17.7521 11.9266 17.75H8.54688V7.90625H14.4039Z" fill="#34D399"/>
				<path d="M14.0156 2.98437H12.7304C12.8552 2.72885 12.9207 2.44842 12.9218 2.16406C12.9218 1.65642 12.7202 1.16957 12.3612 0.810614C12.0023 0.451659 11.5154 0.25 11.0078 0.25H10.7343C10.1925 0.250396 9.65927 0.384949 9.18214 0.641645C8.70501 0.898342 8.29887 1.26919 7.99997 1.72109C7.70108 1.26919 7.29494 0.898342 6.81781 0.641645C6.34068 0.384949 5.80741 0.250396 5.26561 0.25L4.99217 0.25C4.48453 0.25 3.99768 0.451659 3.63873 0.810614C3.27977 1.16957 3.07812 1.65642 3.07812 2.16406C3.07928 2.44842 3.14472 2.72885 3.26952 2.98437H1.98437C1.54925 2.98437 1.13195 3.15722 0.824276 3.46489C0.516601 3.77257 0.34375 4.18987 0.34375 4.62499V5.17186C0.34375 5.60698 0.516601 6.02428 0.824276 6.33195C1.13195 6.63963 1.54925 6.81248 1.98437 6.81248H14.0156C14.4507 6.81248 14.868 6.63963 15.1757 6.33195C15.4833 6.02428 15.6562 5.60698 15.6562 5.17186V4.62499C15.6562 4.18987 15.4833 3.77257 15.1757 3.46489C14.868 3.15722 14.4507 2.98437 14.0156 2.98437ZM10.7343 1.34375H11.0078C11.2253 1.34375 11.434 1.43017 11.5878 1.58401C11.7417 1.73785 11.8281 1.9465 11.8281 2.16406C11.8281 2.38162 11.7417 2.59026 11.5878 2.7441C11.434 2.89794 11.2253 2.98437 11.0078 2.98437H8.61794C8.73911 2.51508 9.0127 2.09932 9.39577 1.80237C9.77883 1.50543 10.2497 1.34411 10.7343 1.34375ZM4.17186 2.16406C4.17186 1.9465 4.25829 1.73785 4.41213 1.58401C4.56596 1.43017 4.77461 1.34375 4.99217 1.34375H5.26561C5.75029 1.34411 6.22112 1.50543 6.60418 1.80237C6.98724 2.09932 7.26084 2.51508 7.38201 2.98437H4.99217C4.77461 2.98437 4.56596 2.89794 4.41213 2.7441C4.25829 2.59026 4.17186 2.38162 4.17186 2.16406Z" fill="#34D399"/>
				</svg></span></div>', 'archive-master'),
					$capability,
					esc_url('https://archivemaster.pro/'),
				];

                // Output 'target="_blank"' for "Upgrade to Pro"
                // phpcs:ignore Universal.FunctionDeclarations.NoLongClosures.ExceedsMaximum
                add_action('admin_footer', function () {
                    ?>
                        <script>
                            document.addEventListener("DOMContentLoaded", function () {
                                const proLink = document.querySelector(`#adminmenu a[href="https://archivemaster.pro/"]`);
                                if (proLink) {
                                    proLink.setAttribute("target", "_blank");
                                }
                            });
                        </script>
                    <?php
                });
            }
        }

        $this->remove_archm_logs_post_type_menu();
    }

    /**
     * Render Main Page
     *
     * @return void
     * @author Siam Maruf
     * @since 1.0
     */
    // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.voidFound
    public function menu_page_template(): void {
        require_once ARCHM_PLUGIN_PATH . '/templates/archive.php';
    }

    // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.voidFound
    public function remove_archm_logs_post_type_menu(): void {
        remove_menu_page('edit.php?post_type=archm_logs');
    }
}
