<?php

namespace Calio;

use Calio\Admin\AdminPage;
use Calio\Admin\DashboardWidget;
use Calio\Admin\DashboardWidgets\OrderProcessingMonitor;
use Calio\Admin\DashboardWidgets\MarketingIntelligenceHud;
use Calio\Admin\DashboardWidgets\InlineProductsManager;
use Calio\Core\Assets;
use Calio\Core\ExtensionManager;
use Calio\Core\GoogleCalendarOAuthManager;
use Calio\Core\MegaMenuManager;
use Calio\Core\ModuleManager;
use Calio\Core\PostTimelineAjaxManager;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Boots Calio services only once and keeps Elementor integration behind guards.
 */
class Plugin
{
    private static ?self $instance = null;

    /**
     * Prevents duplicate hook registration during repeated bootstrap calls.
     */
    private bool $booted = false;
    private bool $elementorServicesBooted = false;

    public static function instance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * Fails activation early when the host environment is below plugin requirements.
     */
    public static function on_activation(): void
    {
        global $wp_version;

        $isPhpSupported = version_compare(PHP_VERSION, '8.2', '>=');
        $isWpSupported = version_compare((string) $wp_version, '6.7', '>=');

        if ($isPhpSupported && $isWpSupported) {
            return;
        }

        if (function_exists('deactivate_plugins')) {
            deactivate_plugins(CALIO_BASENAME);
        }

        wp_die(
            esc_html__('Calio requires WordPress 6.7+ and PHP 8.2+.', 'calio-addon'),
            esc_html__('Plugin Activation Error', 'calio-addon'),
            ['back_link' => true]
        );
    }

    /**
     * Registers runtime services after Elementor has loaded.
     */
    public function boot(): void
    {
        if ($this->booted) {
            return;
        }

        $this->booted = true;

        new DashboardWidget();
        new OrderProcessingMonitor();
        new MarketingIntelligenceHud();
        new InlineProductsManager();
        new GoogleCalendarOAuthManager();
        $this->boot_elementor_services();

        if (!did_action('elementor/loaded') && !class_exists('\Elementor\Plugin')) {
            add_action('admin_notices', [$this, 'render_elementor_missing_notice']);
        }
    }

    /**
     * Boots all Elementor-dependent services once Elementor is ready.
     */
    public function boot_elementor_services(): void
    {
        if ($this->elementorServicesBooted) {
            return;
        }

        $this->elementorServicesBooted = true;

        new Assets();
        new AdminPage();
        new MegaMenuManager();
        new ModuleManager();
        new PostTimelineAjaxManager();
        new ExtensionManager();

        add_action('elementor/elements/categories_registered', [$this, 'register_widget_category']);
    }

    /**
     * Adds a dedicated Elementor panel category for Calio widgets.
     *
     * @param object $elementsManager Elementor category manager instance.
     */
    public function register_widget_category($elementsManager): void
    {
        $elementsManager->add_category(
            'calio-widgets',
            [
                'title' => esc_html__('Calio Widgets', 'calio-addon'),
                'icon' => 'fa fa-plug',
            ]
        );
    }

    /**
     * Shows a capability-gated admin notice when Elementor is unavailable.
     */
    public function render_elementor_missing_notice(): void
    {
        if (!current_user_can('activate_plugins')) {
            return;
        }

        $message = esc_html__('Calio Addon requires Elementor to be installed and activated.', 'calio-addon');

        echo '<div class="notice notice-error"><p>' . esc_html($message) . '</p></div>';
    }

}
