<?php

declare(strict_types=1);

namespace CloudXM\NFe\Providers;

use CloudXM\NFe\Bootstrap\Container;

/**
 * Admin Service Provider
 *
 * Registers admin-related services and hooks
 */
class AdminServiceProvider
{
    public function __construct(
        private Container $container
    ) {}

    /**
     * Register admin services
     */
    public function register(): void
    {
        // Register NFe Invoices Page
        $this->container->singleton(\CloudXM\NFe\Admin\Pages\NFeInvoicesPage::class, function ($c) {
            return new \CloudXM\NFe\Admin\Pages\NFeInvoicesPage(
                $c->get(\CloudXM\NFe\Persistence\NfeEmissionsRepository::class),
                $c->get(\CloudXM\NFe\Services\NFeFileStorageService::class)
            );
        });

        // Register NFe Settings Page
        $this->container->singleton(\CloudXM\NFe\Admin\Pages\SettingsPage::class, function ($c) {
            return new \CloudXM\NFe\Admin\Pages\SettingsPage(
                $c->get(\CloudXM\NFe\Config\Configuration::class),
                $c->get(\CloudXM\NFe\Services\ApiClient::class),
                $c->get(\CloudXM\NFe\Services\TenantConfigCache::class)
            );
        });

        // Register NFe AJAX Controller
        $this->container->singleton(\CloudXM\NFe\Admin\Controllers\NFeAjaxController::class, function ($c) {
            return new \CloudXM\NFe\Admin\Controllers\NFeAjaxController(
                $c->get(\CloudXM\NFe\Persistence\NfeEmissionsRepository::class),
                $c->get(\CloudXM\NFe\Services\NFeFileStorageService::class),
                $c->get(\CloudXM\NFe\Services\ApiClient::class),
                $c->get(\CloudXM\NFe\Services\OrderInvoiceLifecycleService::class)
            );
        });

        // Register NFe Status Poll Controller
        $this->container->singleton(\CloudXM\NFe\Admin\Controllers\NFeStatusPollController::class);

        // Register modern order metabox controller
        $this->container->singleton(\CloudXM\NFe\Admin\Controllers\OrderMetaBoxController::class, function ($c) {
            return new \CloudXM\NFe\Admin\Controllers\OrderMetaBoxController(
                $c->get(\CloudXM\NFe\Services\OrderInvoiceLifecycleService::class),
                $c->get(\CloudXM\NFe\Config\Configuration::class),
                $c->get(\CloudXM\NFe\Services\NFeAutomationService::class)
            );
        });

        // Register Admin Notice Controller
        $this->container->singleton(\CloudXM\NFe\Admin\Controllers\AdminNoticeController::class);
    }

    /**
     * Boot admin services
     */
    public function boot(): void
    {
        add_action('admin_menu', [$this, 'registerAdminMenu']);

        // Eagerly instantiate pages on admin_init so their constructor-time hooks
        // (admin_post, wp_ajax, admin_enqueue_scripts) are registered before
        // admin-post.php or ajax handlers fire. registerAdminMenu() also instantiates
        // them for render callbacks, but admin-post.php skips admin_menu entirely.
        add_action('admin_init', function () {
            $this->container->get(\CloudXM\NFe\Admin\Pages\SettingsPage::class);
        });

        // AJAX handlers must be registered on every admin page (they only fire on POST)
        add_action('admin_init', function () {
            $this->container->get(\CloudXM\NFe\Admin\Controllers\NFeAjaxController::class);
            $this->container->get(\CloudXM\NFe\Admin\Controllers\NFeStatusPollController::class);
        });

        // Order metabox must be registered on every admin page (hooks are conditional internally)
        add_action('admin_init', function () {
            $this->container->get(\CloudXM\NFe\Admin\Controllers\OrderMetaBoxController::class);
        });

        // Admin notices must be registered on every admin page
        add_action('admin_init', function () {
            $this->container->get(\CloudXM\NFe\Admin\Controllers\AdminNoticeController::class);
        });

        // Pages are lazy-loaded via render callbacks in registerAdminMenu()
    }

    /**
     * Register admin menu
     */
    public function registerAdminMenu(): void
    {
        add_menu_page(
            __('CloudXM NFe', 'woocommerce-cloudxm-nfe'),
            __('CloudXM NFe', 'woocommerce-cloudxm-nfe'),
            'manage_options',
            'cloudxm-nfe',
            [$this, 'renderInvoicesPage'],
            'dashicons-media-text',
            60
        );

        add_submenu_page(
            'cloudxm-nfe',
            __('NFe Invoices', 'woocommerce-cloudxm-nfe'),
            __('Invoices', 'woocommerce-cloudxm-nfe'),
            'manage_options',
            'cloudxm-nfe',
            [$this, 'renderInvoicesPage']
        );

        add_submenu_page(
            'cloudxm-nfe',
            __('Settings', 'woocommerce-cloudxm-nfe'),
            __('Settings', 'woocommerce-cloudxm-nfe'),
            'manage_options',
            'cloudxm-nfe-settings',
            [$this, 'renderSettingsPage']
        );

        // Now that the menu is registered, eagerly load the pages so their
        // admin_enqueue_scripts hooks are registered in time. This only runs on admin pages.
        $this->container->get(\CloudXM\NFe\Admin\Pages\NFeInvoicesPage::class);
        $this->container->get(\CloudXM\NFe\Admin\Pages\SettingsPage::class);
    }

    /**
     * Render invoices page
     */
    public function renderInvoicesPage(): void
    {
        $page = $this->container->get(\CloudXM\NFe\Admin\Pages\NFeInvoicesPage::class);
        $page->render();
    }

    /**
     * Render settings page
     */
    public function renderSettingsPage(): void
    {
        $page = $this->container->get(\CloudXM\NFe\Admin\Pages\SettingsPage::class);
        $page->render();
    }
}
