<?php
/**
 * SettingsModule
 *
 * Exposes settings management as a proper module:
 *   • Boots the Settings service (already initialised by Plugin::init, this
 *     just registers admin-facing hooks).
 *   • Registers all AJAX handlers for saving / importing / exporting settings.
 *   • Registers admin pages through the ultraguard_register_admin_pages action.
 *
 * Settings data itself lives in modules\settings\Settings (the service class).
 * This module is the "entry point" that connects that service to WordPress.
 *
 * @package UltraguardPro\modules\settings
 */

namespace UltraguardPro\modules\settings;

use UltraguardPro\modules\core\AbstractModule;

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

class SettingsModule extends AbstractModule {

    public function get_id(): string { return 'settings'; }

    public function is_enabled(): bool { return true; }

    public function boot(): void {
        // The Settings service is already instantiated by Plugin::init().
        // We wire only the actions that this module genuinely owns:
        // export / import / reset / validate / test / single-field read-write.
        //
        // The consolidated dashboard settings screen still needs one shared
        // save route for its top-level form submit.
        $service = $this->plugin->settings;
        $ajax    = $this->plugin->ajax;

        $owned_actions = [
            'ultraguard_save_consolidated_settings' => [ $service, 'handle_ajax_settings' ],
            'ultraguard_export_all_settings'     => [ $service, 'handle_ajax_settings' ],
            'ultraguard_reset_all_settings'      => [ $service, 'handle_ajax_settings' ],
            'ultraguard_import_settings'         => [ $service, 'handle_ajax_settings' ],
            'ultraguard_validate_settings'       => [ $service, 'handle_ajax_settings' ],
            'ultraguard_test_all_configurations' => [ $service, 'handle_ajax_settings' ],
            'ultraguard_get_setting'             => [ $service, 'handle_ajax_settings' ],
            'ultraguard_update_setting'          => [ $service, 'handle_ajax_settings' ],
        ];

        foreach ( $owned_actions as $action => $cb ) {
            $ajax->register_action( $action, $cb );
        }

        // Public login-page UX settings (CAPTCHA, 2FA, Passkeys) are served by
        // the Settings service itself because it owns the allowlist and optional
        // nonce logic. Register a direct guest hook here so the live module
        // bootstrap matches that contract instead of forcing the request through
        // AjaxManager's admin-oriented nonce/capability pipeline.
        add_action( 'wp_ajax_nopriv_ultraguard_get_setting', [ $service, 'handle_ajax_settings' ] );

        // Settings page registration handled via each module's own Settings subpage.
        add_action( 'ultraguard_register_admin_pages', [ $this, 'register_pages' ], 20, 1 );

        // Schedule periodic settings cleanup via this module (not the service class).
        $this->schedule_settings_cleanup( $service );

    }

    /**
     * Schedule the settings cleanup cron if not already registered.
     * Ownership lives here in the module, not in the Settings service class.
     */
    private function schedule_settings_cleanup( $service ): void {
        if ( ! wp_next_scheduled( 'ultraguard_pro_settings_cleanup' ) ) {
            wp_schedule_event( time(), 'daily', 'ultraguard_pro_settings_cleanup' );
        }
        add_action( 'ultraguard_pro_settings_cleanup', [ $service, 'perform_cleanup' ] );
    }

    public function register_pages( $admin ): void {
        // Settings page removed — each module now has its own dedicated Settings subpage.
        // Firewall → firewall-settings, LoginLimiter → login-limiter-settings, etc.
    }
}
