<?php
/**
 * Audit Log Observer Registry
 *
 * Loads and initialises observer classes based on the current logging mode.
 * Called once during plugins_loaded (priority 15) via vmpfence.php.
 *
 * @package VMPFence
 * @since 2.2.5
 */

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

class VMPFence_Audit_Log_Observer_Registry {

    /**
     * Registered observer instances.
     *
     * @var VMPFence_Audit_Log_Observer[]
     */
    private static $observers = array();

    /**
     * Initialise observers based on current logging mode.
     *
     * - disabled  → no observers loaded at all
     * - preview / significant → User, Site, VMPFence (+ Multisite if applicable)
     * - all       → above + Content observer
     *
     * @return void
     */
    public static function init() {
        $mode = get_option('vmpfence_audit_log_logging_mode', 'preview');

        // Disabled mode — don't register any hooks
        if ($mode === 'disabled') {
            return;
        }

        // Core observers (always loaded when not disabled)
        $observer_classes = array(
            'VMPFence_Audit_Log_Observer_User',
            'VMPFence_Audit_Log_Observer_Site',
            'VMPFence_Audit_Log_Observer_VMPFence',
        );

        // Content observer only in "all" mode (too noisy otherwise)
        if ($mode === 'all') {
            $observer_classes[] = 'VMPFence_Audit_Log_Observer_Content';
        }

        // Multisite observer only on multisite installs
        if (is_multisite()) {
            $observer_classes[] = 'VMPFence_Audit_Log_Observer_Multisite';
        }

        /**
         * Filter the list of observer classes before instantiation.
         *
         * @since 2.2.5
         * @param string[] $observer_classes Fully-qualified class names.
         * @param string   $mode             Current logging mode.
         */
        $observer_classes = apply_filters('vmpfence_audit_log_observers', $observer_classes, $mode);

        foreach ($observer_classes as $class) {
            if (class_exists($class)) {
                $observer = new $class();
                $observer->register_hooks();
                self::$observers[] = $observer;
            }
        }
    }

    /**
     * Get all registered observer instances.
     * Useful for debugging / verification.
     *
     * @return VMPFence_Audit_Log_Observer[]
     */
    public static function get_observers() {
        return self::$observers;
    }

    /**
     * Reset observers (primarily for testing).
     *
     * @return void
     */
    public static function reset() {
        self::$observers = array();
    }
}
