<?php

namespace UltraguardPro\modules\login_protect\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\login_protect\LoginUrlEngine;

class LoginProtectAjax extends AbstractAjaxHandler {

    protected const NONCE = 'ultraguard_admin_nonce';

    public function register(): void {
        $this->register_actions( [
            'ultraguard_login_protect_generate_path' => [ $this, 'handle_generate_path' ],
            'ultraguard_login_protect_get_stats'     => [ $this, 'handle_get_stats' ],
            'ultraguard_login_protect_clear_stats'   => [ $this, 'handle_clear_stats' ],
            'ultraguard_save_login_protect_settings' => [ $this, 'handle_save_settings' ],
        ] );
    }

    public function handle_generate_path(): void {
        $path = ( new LoginUrlEngine( $this->plugin ) )->generate_secret_path();
        $this->send_success( [ 'path' => $path ] );
    }

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

    public function handle_clear_stats(): void {
        ( new LoginUrlEngine( $this->plugin ) )->clear_stats();
        $this->send_success( [ 'message' => __( 'Login protect stats cleared.', 'ultraguard-security-suite' ) ] );
    }

    public function handle_save_settings(): void {
        $data = $this->sanitize_fields( (array) wp_unslash( $_POST ), [
            'login_protect_enabled'              => [ 'type' => 'bool', 'default' => true ],
            'login_protect_custom_path'          => [ 'type' => 'text', 'default' => '' ],
            'login_protect_original_action'      => [ 'type' => 'select', 'default' => '404', 'options' => [ '404', 'redirect_home', 'redirect_custom' ] ],
            'login_protect_custom_redirect_url'  => [ 'type' => 'text', 'default' => '' ],
            'login_protect_log_attempts'         => [ 'type' => 'bool', 'default' => true ],
            'login_protect_auto_block'           => [ 'type' => 'bool', 'default' => false ],
            'login_protect_auto_block_threshold' => [ 'type' => 'int', 'default' => 5, 'min' => 1, 'max' => 100 ],
            'login_protect_notify_admin'         => [ 'type' => 'bool', 'default' => false ],
        ] );

        if ( ! empty( $data['login_protect_custom_path'] ) ) {
            $data['login_protect_custom_path'] = sanitize_title( $data['login_protect_custom_path'] );
        }

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

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