<?php

namespace UltraguardPro\modules\reputation_monitor\ajax;

if ( ! defined( 'ABSPATH' ) ) { exit; }
// phpcs:disable WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended -- Requests are routed through the shared protected AJAX layer.

use UltraguardPro\modules\core\AbstractAjaxHandler;
use UltraguardPro\modules\reputation_monitor\ReputationCheckEngine;

class ReputationMonitorAjax extends AbstractAjaxHandler {

    protected const NONCE = 'ultraguard_admin_nonce';

    private ?ReputationCheckEngine $engine = null;

    public function register(): void {
        $this->register_actions( [
            'ultraguard_reputation_check_now'             => [ $this, 'handle_check_now' ],
            'ultraguard_reputation_get_results'           => [ $this, 'handle_get_results' ],
            'ultraguard_reputation_get_history'           => [ $this, 'handle_get_history' ],
            'ultraguard_reputation_get_stats'             => [ $this, 'handle_get_stats' ],
            'ultraguard_save_reputation_monitor_settings' => [ $this, 'handle_save_settings' ],
        ] );
    }

    public function handle_check_now(): void {
        $this->require_pro_feature( 'reputation_monitor', __( 'run a blacklist and reputation check', 'ultraguard-security-suite' ) );
        $this->send_success( $this->engine()->run_all_checks() );
    }

    public function handle_get_results(): void {
        $this->send_success( [
            'results'        => $this->engine()->get_check_summary(),
            'overall_status' => $this->engine()->get_overall_status(),
        ] );
    }

    public function handle_get_history(): void {
        $limit = max( 10, min( 200, absint( $_POST['limit'] ?? 50 ) ) );
        $this->send_success( [ 'history' => $this->engine()->get_history( $limit ) ] );
    }

    public function handle_get_stats(): void {
        $this->send_success( $this->engine()->get_stats() );
    }

    public function handle_save_settings(): void {
        $this->require_pro_feature( 'reputation_monitor', __( 'save Reputation Monitor settings', 'ultraguard-security-suite' ) );

        $data = $this->sanitize_fields( (array) wp_unslash( $_POST ), [
            'reputation_monitor_enabled'           => [ 'type' => 'bool', 'default' => true ],
            'reputation_monitor_google_api_key'    => [ 'type' => 'text', 'default' => '' ],
            'reputation_monitor_enable_spamhaus'   => [ 'type' => 'bool', 'default' => true ],
            'reputation_monitor_enable_mxtoolbox'  => [ 'type' => 'bool', 'default' => false ],
            'reputation_monitor_mxtoolbox_api_key' => [ 'type' => 'text', 'default' => '' ],
            'reputation_monitor_enable_mcafee'     => [ 'type' => 'bool', 'default' => true ],
            'reputation_monitor_enable_norton'     => [ 'type' => 'bool', 'default' => true ],
            'reputation_monitor_enable_bing'       => [ 'type' => 'bool', 'default' => true ],
            'reputation_monitor_frequency'         => [ 'type' => 'select', 'default' => 'hourly', 'options' => [ 'every_30_minutes', 'hourly', 'every_3_hours', 'every_6_hours', 'every_12_hours', 'daily' ] ],
            'reputation_monitor_email_alert'       => [ 'type' => 'bool', 'default' => true ],
            'reputation_monitor_weekly_report'     => [ 'type' => 'bool', 'default' => false ],
            'reputation_monitor_alert_recipients'  => [ 'type' => 'array', 'default' => [] ],
            'reputation_monitor_webhook_alert'     => [ 'type' => 'bool', 'default' => false ],
            'reputation_monitor_webhook_url'       => [ 'type' => 'text', 'default' => '' ],
        ] );

        $ok = $this->plugin->settings->save_module_settings( 'reputation_monitor', $data );
        if ( ! $ok ) {
            $this->send_error( __( 'Failed to save Reputation Monitor settings.', 'ultraguard-security-suite' ), 500, 'save_failed' );
        }

        $module = $this->plugin->modules->get( 'reputation_monitor' );
        if ( $module && method_exists( $module, 'maybe_setup_schedule' ) ) {
            $module->maybe_setup_schedule();
        }

        $this->send_success( [ 'message' => __( 'Reputation Monitor settings saved.', 'ultraguard-security-suite' ) ] );
    }

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

        return $this->engine;
    }
}
