<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

namespace bbbext_bnx;

use bbbext_bnx\local\sidecar_state_manager;
use core\plugininfo\mod;

/**
 * Event observer callbacks for BN Experience extension.
 *
 * @package    bbbext_bnx
 * @copyright  2026 onwards, Blindside Networks Inc
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author     Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
 */
class observer {
    /**
     * Handle config log changes and cascade BNX sidecar status on BNX plugin state changes.
     *
     * @param \core\event\config_log_created $event
     * @return void
     */
    public static function config_log_created(\core\event\config_log_created $event): void {
        $other = $event->other ?? [];

        if (($other['name'] ?? '') !== 'disabled') {
            return;
        }

        if (($other['plugin'] ?? '') !== 'bbbext_bnx') {
            return;
        }

        $bnxdisabled = (int)($other['value'] ?? 0) === 1;

        if (!$bnxdisabled) {
            require_once(__DIR__ . '/../db/migration.php');

            // Ensure BigBlueButtonBN module is enabled when BNX is enabled.
            mod::enable_plugin('bigbluebuttonbn', 1);

            // Refresh core lock settings into BNX on every enable.
            bbbext_bnx_sync_core_locksettings_data();

            // BNX owns reminders when enabled; force-disable legacy bnreminders.
            self::disable_bnreminders_if_enabled();
        }

        sidecar_state_manager::apply_for_bnx_state($bnxdisabled);
    }

    /**
     * Disable bnreminders if it is currently enabled.
     *
     * @return void
     */
    private static function disable_bnreminders_if_enabled(): void {
        $bnreminders = \core_plugin_manager::instance()->get_plugin_info('bbbext_bnreminders');
        if (!$bnreminders || !$bnreminders->is_enabled()) {
            return;
        }

        $oldvalue = get_config('bbbext_bnreminders', 'disabled');
        if (!empty($oldvalue)) {
            return;
        }

        set_config('disabled', 1, 'bbbext_bnreminders');
        add_to_config_log('disabled', $oldvalue, 1, 'bbbext_bnreminders');
        \core_plugin_manager::reset_caches();
    }

    /**
     * React to subplugin state changes via generic callback discovery.
     *
     * When any bbbext subplugin is enabled, this observer checks whether the
     * plugin defines a `\<plugin>\plugininfo_callbacks::on_enable()` method
     * and invokes it. This allows each sidecar to declare its own enable-time
     * behaviour without requiring changes to the parent plugin.
     *
     * @param \core\event\config_log_created $event
     * @return void
     */
    public static function subplugin_config_log_created(\core\event\config_log_created $event): void {
        $other = $event->other ?? [];

        if (($other['name'] ?? '') !== 'disabled') {
            return;
        }

        $plugin = $other['plugin'] ?? '';
        $disabled = (int)($other['value'] ?? 0) === 1;

        // Only act on enable events for bbbext plugins.
        if ($disabled || strpos($plugin, 'bbbext_') !== 0) {
            return;
        }

        // Generic callback discovery: invoke the sidecar's on_enable() if defined.
        $callbackclass = '\\' . $plugin . '\\plugininfo_callbacks';
        if (class_exists($callbackclass) && method_exists($callbackclass, 'on_enable')) {
            $callbackclass::on_enable();
        }
    }
}
