<?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;

use BookiFlex\Core\Contract\ExtensionInterface;
use BookiFlex\Core\Contract\PluginInterface;

defined( 'ABSPATH' ) || exit;

/**
 * Onboarding Extension
 *
 * Provides first-time setup wizard functionality for new BookiFlex installations.
 * Supports three modes: Quick Setup, Test Data, and Manual Configuration.
 *
 * @package BookiFlex\Extensions\Onboarding
 * @since   1.0.0
 */
class Onboarding implements ExtensionInterface
{
    /**
     * Initialize the extension
     *
     * @param  PluginInterface $plugin The plugin instance
     * @return void
     */
    public function init(PluginInterface $plugin): void
    {
        // Register services first — need OnboardingStateManager for the check below
        $plugin->getContainer()->register(new ServiceProvider());

        /** @var Service\OnboardingStateManager $stateManager */
        $stateManager = $plugin->getContainer()[Service\OnboardingStateManager::class];

        // If onboarding is already completed, skip everything —
        // no menus, no controllers, no assets, no Freemius filter needed.
        if ($stateManager->isCompleted()) {
            return;
        }

        // Suppress Freemius activation redirect while onboarding is in progress.
        // After onboarding completes, the user is redirected to bookiflex-settings,
        // which Freemius overrides with its opt-in page (override_plugin_menu_with_activation).
        $plugin->fs()->add_filter('redirect_on_activation', '__return_false');

        // Suppress Freemius "one step away" activation notice during onboarding.
        // The notice type 'update-nag' is unique to the activation prompt.
        $plugin->fs()->add_filter('show_admin_notice', function (bool $show, array $msg): bool {
            if ($msg['type'] === 'update-nag') {
                return false;
            }
            return $show;
        }, 10, 2);

        // Register API controllers and menus
        $plugin->addConfig(
            [
                'menus' => [
                    Admin\Menu\OnboardingMenu::class,
                ],
                'controllers' => [
                    Api\Controller\OnboardingController::class,
                ],
            ]
        );

        // Register admin page assets
        $adminPage = new Admin\Page\OnboardingPage($plugin->getAssetsManager(), $plugin);
        $adminPage->init();
    }

    /**
     * Get extension name
     *
     * @return string Extension name
     */
    public function getName(): string
    {
        return 'Onboarding';
    }

    /**
     * Get extension version
     *
     * @return string Extension version
     */
    public function getVersion(): string
    {
        return '1.0.0';
    }

    /**
     * Check if extension is compatible with plugin version
     *
     * @param  string $pluginVersion Plugin version
     * @return bool True if compatible
     */
    public function isCompatible(string $pluginVersion): bool
    {
        return version_compare($pluginVersion, '1.0.0', '>=');
    }
}
