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

use BookiFlex\Core\Admin\AbstractAssetsManager;
use BookiFlex\Core\Admin\AssetContext;
use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Core\Util\Request;

defined( 'ABSPATH' ) || exit;

/**
 * Onboarding Page
 *
 * Handles asset enqueuing for Vue.js onboarding application.
 */
class OnboardingPage
{
    private AbstractAssetsManager $assetsManager;
    private PluginInterface $plugin;

    public function __construct(AbstractAssetsManager $assetsManager, PluginInterface $plugin)
    {
        $this->assetsManager = $assetsManager;
        $this->plugin = $plugin;
    }

    public function init(): void
    {
        $this->plugin->addAction('admin_enqueue_scripts', [$this, 'enqueueAssets']);
    }

    /**
     * Enqueue Vue.js app assets
     */
    public function enqueueAssets(string $hook): void
    {
        // Only load on onboarding page
        if (Request::getString('page') !== 'bookiflex-onboarding') {
            return;
        }

        $this->assetsManager->enqueueExtensionModule('onboarding', [
            'context' => AssetContext::ADMIN,
            'data' => [
                'restUrl' => rest_url('bflex/v1/onboarding'),
                'nonce' => wp_create_nonce('wp_rest'),
                'adminUrl' => admin_url(),
                'settingsUrl' => admin_url('admin.php?page=bookiflex-settings'),
            ],
        ]);

        $this->assetsManager->loadExtensionTranslations('onboarding', AssetContext::ADMIN);
    }
}
