<?php
/**
 * BookiFlex Onboarding Extension
 *
 * @package    BookiFlex
 * @subpackage Onboarding
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\Onboarding\Admin\Menu;

use BookiFlex\Core\Admin\Menu\AbstractMenu;
use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Core\Util\Request;
use BookiFlex\Extensions\Onboarding\Service\OnboardingStateManager;

use function add_submenu_page;
use function admin_url;
use function remove_menu_page;
use function wp_safe_redirect;

defined( 'ABSPATH' ) || exit;

/**
 * Onboarding Menu
 *
 * Registers hidden admin page for onboarding wizard and handles
 * activation redirect to onboarding page.
 */
class OnboardingMenu extends AbstractMenu
{
    public function __construct(
        private readonly OnboardingStateManager $stateManager
    ) {
        // Hook will be registered in register() method
    }

    public function register(PluginInterface $plugin): void
    {
        parent::register($plugin);
        $this->plugin->addAction('admin_init', [$this, 'handleRedirect']);
        $this->plugin->addAction('admin_menu', [$this, 'removeCarbonFieldsMenus'], 99);
    }

    /**
     * Register hidden admin menu page
     */
    public function registerMenu(): void
    {
        // Only show if onboarding not completed
        if ($this->stateManager->isCompleted()) {
            return;
        }

        // CPT submenus (parent: bookiflex) — slug includes full edit.php?post_type=...
        remove_submenu_page('bookiflex', 'edit.php?post_type=bflex-rental-unit');
        remove_submenu_page('bookiflex', 'edit.php?post_type=bflex-cancel-policy');
        remove_submenu_page('bookiflex', 'edit.php?post_type=bflex-payment-type');
        remove_submenu_page('bookiflex', 'edit.php?post_type=bflex-rate-plan');

        // Reservations top-level menu with all submenus (Reservations, Customers, Payments)
        remove_menu_page('bookiflex-reservations');

        add_submenu_page(
            'bookiflex', // Hidden from menu (empty parent)
            __('Install wizard', 'bookiflex'),
            __('Install wizard', 'bookiflex'),
            'manage_options',
            'bookiflex-onboarding',
            [$this, 'renderPage']
        );
    }

    /**
     * Handle redirect after activation
     */
    public function handleRedirect(): void
    {
        if (!$this->stateManager->needsRedirect()) {
            return;
        }

        // If onboarding already completed, just clean up the stale flag
        if ($this->stateManager->isCompleted()) {
            $this->stateManager->clearRedirectFlag();
            return;
        }

        // Don't redirect on bulk activation
        if (Request::has('activate-multi')) {
            return;
        }

        // Don't redirect if already on onboarding page
        if (Request::getString('page') === 'bookiflex-onboarding') {
            return;
        }

        wp_safe_redirect(admin_url('admin.php?page=bookiflex-onboarding'));
        exit;
    }

    /**
     * Render onboarding page (Vue.js app container)
     */
    public function renderPage(): void
    {
        echo '<div id="bookiflex-onboarding-app"></div>';
    }

    /**
     * Remove Carbon Fields menus at late priority
     *
     * Executes at priority 99 to ensure Carbon Fields has already
     * registered its menus via the admin_menu hook.
     */
    public function removeCarbonFieldsMenus(): void
    {
        // Only remove if onboarding not completed
        if ($this->stateManager->isCompleted()) {
            return;
        }

        remove_submenu_page('bookiflex', 'bookiflex-settings');
        remove_submenu_page('bookiflex', 'bookiflex-notifications');
    }
}
