<?php

namespace UltraguardPro\modules\loginlimiter\pages;

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

use UltraguardPro\Plugin;
use UltraguardPro\modules\core\BasePage;

class LoginProtectSettings extends BasePage {

    public function __construct( Plugin $plugin ) {
        parent::__construct( $plugin );
        $this->ajax_nonce_action = 'ultraguard_admin_nonce';
    }

    public function get_scheme( $context ): array {
        $s = [
            'login_protect_enabled'              => $this->plugin->settings->get_option( 'login_protect_enabled', true ),
            'login_protect_custom_path'          => $this->plugin->settings->get_option( 'login_protect_custom_path', '' ),
            'login_protect_original_action'      => $this->plugin->settings->get_option( 'login_protect_original_action', '404' ),
            'login_protect_custom_redirect_url'  => $this->plugin->settings->get_option( 'login_protect_custom_redirect_url', '' ),
            'login_protect_log_attempts'         => $this->plugin->settings->get_option( 'login_protect_log_attempts', true ),
            'login_protect_auto_block'           => $this->plugin->settings->get_option( 'login_protect_auto_block', false ),
            'login_protect_auto_block_threshold' => $this->plugin->settings->get_option( 'login_protect_auto_block_threshold', 5 ),
            'login_protect_notify_admin'         => $this->plugin->settings->get_option( 'login_protect_notify_admin', false ),
        ];

        return [
            'type'      => 'page',
            'id'        => 'login-protect-settings',
            'page_slug' => 'login-protect-settings',
            'header'    => [
                'title'    => __( 'Login URL Protection Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Replace wp-login.php with a secret path and control how the original URLs behave.', 'ultraguard-security-suite' ),
                'actions'  => [
                    $this->create_url_button( __( 'Back to Login Protection', 'ultraguard-security-suite' ), $this->get_page_url( 'login-protect' ), 'secondary', 'dashicons-arrow-left-alt' ),
                ],
            ],
            'data'      => [
                [ 'type' => 'card', 'title' => __( 'Custom Login Path', 'ultraguard-security-suite' ), 'icon' => 'dashicons-admin-links', 'data' => [
                    [ 'type' => 'text', 'name' => 'login_protect_custom_path', 'label' => __( 'Custom login path slug', 'ultraguard-security-suite' ), 'value' => (string) $s['login_protect_custom_path'] ],
                    [ 'type' => 'button', 'text' => __( 'Generate Random', 'ultraguard-security-suite' ), 'button_style' => 'secondary', 'ajax' => [ 'action' => 'ultraguard_login_protect_generate_path', 'nonce' => $this->create_nonce( 'ultraguard_admin_nonce' ), 'data' => [] ] ],
                ] ],
                [ 'type' => 'card', 'title' => __( 'Original URL Action', 'ultraguard-security-suite' ), 'icon' => 'dashicons-warning', 'data' => [
                    [ 'type' => 'select', 'name' => 'login_protect_original_action', 'label' => __( 'Action on original URL hit', 'ultraguard-security-suite' ), 'value' => (string) $s['login_protect_original_action'], 'options' => [
                        [ 'value' => '404', 'label' => __( '404 Not Found', 'ultraguard-security-suite' ) ],
                        [ 'value' => 'redirect_home', 'label' => __( 'Redirect to home', 'ultraguard-security-suite' ) ],
                        [ 'value' => 'redirect_custom', 'label' => __( 'Redirect to custom URL', 'ultraguard-security-suite' ) ],
                    ] ],
                    [ 'type' => 'text', 'name' => 'login_protect_custom_redirect_url', 'label' => __( 'Custom redirect URL', 'ultraguard-security-suite' ), 'value' => (string) $s['login_protect_custom_redirect_url'] ],
                ] ],
                [ 'type' => 'card', 'title' => __( 'Monitoring & Blocking', 'ultraguard-security-suite' ), 'icon' => 'dashicons-shield', 'data' => [
                    [ 'type' => 'toggle', 'name' => 'login_protect_enabled', 'label' => __( 'Enable Login URL Protection', 'ultraguard-security-suite' ), 'value' => ! empty( $s['login_protect_enabled'] ) ],
                    [ 'type' => 'toggle', 'name' => 'login_protect_log_attempts', 'label' => __( 'Log access attempts to the old URL', 'ultraguard-security-suite' ), 'value' => ! empty( $s['login_protect_log_attempts'] ) ],
                    [ 'type' => 'toggle', 'name' => 'login_protect_auto_block', 'label' => __( 'Auto-block repeated hits to the old URL', 'ultraguard-security-suite' ), 'value' => ! empty( $s['login_protect_auto_block'] ) ],
                    [ 'type' => 'number', 'name' => 'login_protect_auto_block_threshold', 'label' => __( 'Auto-block threshold per hour', 'ultraguard-security-suite' ), 'value' => (int) $s['login_protect_auto_block_threshold'], 'min' => 1, 'max' => 100 ],
                    [ 'type' => 'toggle', 'name' => 'login_protect_notify_admin', 'label' => __( 'Notify admin when old URL is accessed', 'ultraguard-security-suite' ), 'value' => ! empty( $s['login_protect_notify_admin'] ) ],
                ] ],
            ],
            'form'      => [
                'enable' => true,
                'id'     => 'login-protect-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_save_login_protect_settings',
                'nonce'  => $this->create_nonce( 'ultraguard_admin_nonce' ),
                'submit' => [ 'text' => __( 'Save Settings', 'ultraguard-security-suite' ), 'button_style' => 'primary', 'icon' => 'dashicons-saved' ],
            ],
        ];
    }
}
