<?php

/**
 * Plugin composition root.
 *
 * @package VoidPulse
 */
namespace VoidPulse\Core;

use VoidPulse\Admin\Admin;
use VoidPulse\Notifications\Analytics;
use VoidPulse\Notifications\RecentSaleCollector;
use VoidPulse\Frontend\Frontend;
use VoidPulse\Infrastructure\Jobs;
use VoidPulse\Privacy\Privacy;
use VoidPulse\Shared\Hooks;
defined( 'ABSPATH' ) || exit;
final class Plugin {
    /**
     * Singleton instance.
     *
     * @var self|null
     */
    private static $instance;

    /**
     * Whether the licensed optional runtime completed its bootstrap.
     *
     * @var bool
     */
    private $optional_runtime_active = false;

    /**
     * Get instance.
     *
     * @return self
     */
    public static function instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Register hooks.
     *
     * @return void
     */
    public function register() {
        add_action( 'init', array($this, 'boot'), 5 );
    }

    /**
     * Report whether optional services are available for this request.
     *
     * @return bool
     */
    public function optional_runtime_active() {
        return $this->optional_runtime_active;
    }

    /**
     * Boot safe services.
     *
     * @return void
     */
    public function boot() {
        OwnershipRegistry::register_base();
        ( new Admin() )->register();
        ( new Privacy() )->register();
        ( new Hooks() )->register();
        if ( !class_exists( 'WooCommerce' ) ) {
            DowngradeSafety::maybe_run( false );
            return;
        }
        $shop_manager = get_role( 'shop_manager' );
        if ( $shop_manager && !$shop_manager->has_cap( 'edit_voidpulse_campaigns' ) ) {
            Activator::add_capabilities();
        }
        ( new Frontend() )->register();
        ( new Analytics() )->register();
        ( new RecentSaleCollector() )->register();
        ( new Jobs() )->register();
        $fs = ( function_exists( 'voidpulse_fs' ) ? voidpulse_fs() : null );
        if ( is_object( $fs ) ) {
        }
        DowngradeSafety::maybe_run( $this->optional_runtime_active );
    }

}
