<?php
/**
 * Main bootstrap class.
 *
 * @package Polanger_Shield
 */

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

require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-db.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-permissions.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-settings.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-context.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-rules.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-activity.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-safe-mode.php';
require_once POLANGER_SHIELD_DIR . 'includes/demo-lock/class-polanger-shield-demo-lock.php';
require_once POLANGER_SHIELD_DIR . 'includes/global-demo/class-polanger-shield-global-demo.php';
require_once POLANGER_SHIELD_DIR . 'includes/global-demo/class-polanger-shield-global-demo-settings.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-page-blocker.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-menu-manager-integration.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-admin-ui.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-ajax.php';
require_once POLANGER_SHIELD_DIR . 'includes/class-polanger-shield-runtime.php';

class Polanger_Shield {

  /**
   * Singleton instance.
   *
   * @var Polanger_Shield|null
   */
  private static $instance = null;

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

    return self::$instance;
  }

  /**
   * Activation callback.
   *
   * @return void
   */
  public static function activate() {
    Polanger_Shield_DB::maybe_create_tables();
  }

  /**
   * Boot addon/plugin.
   *
   * @return void
   */
  public function boot() {
    add_filter( 'pdt_register_addons', array( $this, 'register_addon_card' ) );

    add_action(
      'polanger_init',
      static function( $loader ) {
        if ( ! is_object( $loader ) || ! method_exists( $loader, 'register_module' ) ) {
          return;
        }

        $loader->register_module(
          array(
            'name'        => 'Polanger Shield',
            'slug'        => 'polanger-shield',
            'version'     => POLANGER_SHIELD_VERSION,
            'requires'    => '1.5.2',
            'description' => 'Block admin pages, lock demo screens, hide visual areas, and leave contextual notes.',
            'page_slug'   => 'polanger-shield',
            'is_core'     => false,
          )
        );
      }
    );

    if ( ! $this->is_polanger_core_available() ) {
      return;
    }

    Polanger_Shield_Settings::instance()->register_hooks();
    Polanger_Shield_Global_Demo_Settings::instance()->register_hooks();
    Polanger_Shield_Safe_Mode::instance()->register_hooks();

    add_action( 'admin_init', array( 'Polanger_Shield_DB', 'maybe_create_tables' ), 5 );

    Polanger_Shield_Admin_UI::instance()->register_hooks();
    Polanger_Shield_Ajax::instance()->register_hooks();
    Polanger_Shield_Runtime::instance()->register_hooks();
    Polanger_Shield_Page_Blocker::instance()->register_hooks();
    Polanger_Shield_Menu_Manager_Integration::instance()->register_hooks();
    Polanger_Shield_Demo_Lock::instance()->register_hooks();
    Polanger_Shield_Global_Demo::instance()->register_hooks();
  }

  /**
   * Register addon card on Polanger Addons page.
   *
   * @param array $addons Existing addon map.
   * @return array
   */
  public function register_addon_card( $addons ) {
    if ( ! is_array( $addons ) ) {
      $addons = array();
    }

    if ( ! isset( $addons['polanger-shield'] ) ) {
      $addons['polanger-shield'] = array(
        'title'       => __( 'Polanger Shield', 'polanger-admin-menu-manager' ),
        'description' => __( 'Block admin pages, lock demo screens, hide selected UI elements, and attach contextual notes for selected users.', 'polanger-admin-menu-manager' ),
        'status'      => 'inactive',
        'slug'        => 'polanger-shield',
        'is_core'     => false,
      );
    }

    return $addons;
  }

  /**
   * Whether Polanger core API is available.
   *
   * @return bool
   */
  private function is_polanger_core_available() {
    return function_exists( 'pdt_get_user_access_level' ) && function_exists( 'pdt_is_addon_active' );
  }

}
