<?php

namespace UltraguardPro\modules\server_intelligence;

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

use UltraguardPro\Plugin;
use UltraguardPro\modules\core\AbstractModule;
use UltraguardPro\support\ServerCommunication;
use UltraguardPro\support\ServerCommunicationAjax;

class ServerIntelligenceModule extends AbstractModule {

    private ?ServerCommunication $server_communication = null;

    public function __construct( Plugin $plugin ) {
        parent::__construct( $plugin );
        $this->plugin->settings->register_module_prefix( 'server_intelligence_', 'server_intelligence' );
    }

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

    public function boot(): void {
        if ( method_exists( $this->plugin->settings, 'register_settings_schema' ) ) {
            $this->plugin->settings->register_settings_schema( 'server_intelligence', 'server_intelligence_', [
                'server_intelligence_enabled' => [ 'type' => 'bool', 'default' => true ],
            ] );
        }

        $this->server_communication()->register();

        if ( is_admin() ) {
            ( new ServerCommunicationAjax( $this->plugin, $this->server_communication() ) )->register();
        }

        add_action( 'ultraguard_register_admin_pages', [ $this, 'register_pages' ], 99, 1 );

        do_action( 'ultraguard_pro/server_intelligence/booted', $this );
    }

    public function register_pages( $admin ): void {
        $admin->register_page( [
            'id'              => 'server-intelligence',
            'page_title'      => __( 'Server Intelligence', 'ultraguard-security-suite' ),
            'menu_title'      => __( 'Intelligence', 'ultraguard-security-suite' ),
            'capability'      => 'manage_options',
            'scheme_callback' => [ new pages\ServerIntelligenceDashboard( $this->plugin ), 'get_scheme' ],
            'parent_id'       => '',
            'priority'        => 99,
            'group'           => 'System',
            'icon'            => 'dashicons-cloud',
        ] );

        $admin->register_page( [
            'id'              => 'server-intelligence-settings',
            'page_title'      => __( 'Server Intelligence Settings', 'ultraguard-security-suite' ),
            'menu_title'      => __( 'Settings', 'ultraguard-security-suite' ),
            'capability'      => 'manage_options',
            'scheme_callback' => [ new pages\ServerIntelligenceSettings( $this->plugin ), 'get_scheme' ],
            'parent_id'       => 'server-intelligence',
            'priority'        => 100,
            'group'           => 'System',
            'icon'            => 'dashicons-admin-settings',
        ] );
    }

    private function server_communication(): ServerCommunication {
        if ( null === $this->server_communication ) {
            $this->server_communication = new ServerCommunication( $this->plugin );
        }

        return $this->server_communication;
    }
}
