<?php
/**
 * NewsSync Extensibility Hooks
 * Permite que addons (plugins externos) se conectem ao NewsSync
 *
 * @package NewsSync
 * @since 1.1.0
 */

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

/**
 * NewsSync Hooks Manager
 * Centraliza hooks de extensibilidade
 */
class NewsSync_Hooks {
    /**
     * Singleton instance
     */
    private static $instance = null;

    /**
     * Registered extensions
     * @var array
     */
    private $extensions = array();

    /**
     * Initialization flag to avoid running init() multiple times
     * @var bool
     */
    private $initialized = false;

    /**
     * Get singleton instance
     */
    public static function instance() {
        if ( null === self::$instance ) {
            // Create and assign the instance first, then run initialization
            // that may call external code. This avoids re-entrancy where
            // external callbacks call instance() during construction.
            self::$instance = new self();
            self::$instance->init();
        }
        return self::$instance;
    }

    /**
     * Constructor
     */
    private function __construct() {
        // Minimal constructor — heavy init that triggers external callbacks
        // is performed in init() after the singleton is assigned.
    }

    /**
     * Initialization that may call external callbacks. Called after the
     * singleton instance has been stored to prevent recursive creation.
     */
    private function init() {
        if ( $this->initialized ) {
            return;
        }
        $this->initialized = true;

        if ( defined( 'NEWSSYNC_VERSION' ) ) {
            do_action( 'newssync/loaded', NEWSSYNC_VERSION );
        } else {
            do_action( 'newssync/loaded', '' );
        }
    }

    /**
     * Register an extension
     * Permite addons se registarem
     *
     * @param string $extension_id Unique ID
     * @param array  $data Extension data (name, version, class, etc.)
     */
    public static function register_extension( $extension_id, $data ) {
        $instance = self::instance();
        $instance->extensions[ $extension_id ] = $data;

        do_action( 'newssync/extension_registered', $extension_id, $data );
    }

    /**
     * Check if extension is active
     *
     * @param string $extension_id Extension ID
     * @return bool
     */
    public static function is_extension_active( $extension_id ) {
        $instance = self::instance();
        return isset( $instance->extensions[ $extension_id ] );
    }

    /**
     * Get all registered extensions
     *
     * @return array
     */
    public static function get_extensions() {
        $instance = self::instance();
        return $instance->extensions;
    }
}

// Initialize after all plugins are loaded to avoid re-entrancy during
// immediate plugin includes (some extensions register on 'newssync/loaded').
add_action( 'plugins_loaded', function() {
    NewsSync_Hooks::instance();
}, 5 );

/**
 * Helper:  Check if any extension is active
 *
 * @return bool
 */
function newssync_has_extensions() {
    $extensions = NewsSync_Hooks::get_extensions();
    return ! empty( $extensions );
}

/**
 * Helper: Check if specific extension is active
 *
 * @param string $extension_id Extension ID
 * @return bool
 */
function newssync_is_extension_active( $extension_id ) {
    return NewsSync_Hooks::is_extension_active( $extension_id );
}
