<?php

namespace UltraguardPro\modules\traffic_monitor\pages;

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

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

class TrafficMonitorSettings extends BasePage {

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

    public function get_scheme( $context ): array {
        $settings = $this->settings->get_module_settings( 'traffic_monitor' );

        return [
            'type'      => 'page',
            'id'        => 'traffic-monitor-settings',
            'page_slug' => 'traffic-monitor-settings',
            'header'    => [
                'title'    => __( 'Traffic Monitor Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Configure request capture, retention, and optional auto-blocking rules.', 'ultraguard-security-suite' ),
                'actions'  => [
                    $this->create_url_button( __( 'Back to Traffic', 'ultraguard-security-suite' ), $this->get_page_url( 'traffic-monitor' ), 'secondary', 'dashicons-arrow-left-alt' ),
                ],
            ],
            'data'      => [
                [
                    'type'  => 'card',
                    'title' => __( 'Logging', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-chart-area',
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'traffic_monitor_enabled', 'label' => __( 'Enable traffic logging', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['traffic_monitor_enabled'] ) ],
                        [ 'type' => 'number', 'name' => 'traffic_monitor_retention_days', 'label' => __( 'Log retention days', 'ultraguard-security-suite' ), 'value' => (int) ( $settings['traffic_monitor_retention_days'] ?? 7 ), 'min' => 1, 'max' => 90 ],
                        [ 'type' => 'toggle', 'name' => 'traffic_monitor_ignore_logged_in', 'label' => __( 'Skip logged-in admins', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['traffic_monitor_ignore_logged_in'] ) ],
                        [ 'type' => 'toggle', 'name' => 'traffic_monitor_skip_localhost', 'label' => __( 'Skip localhost / 127.0.0.1', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['traffic_monitor_skip_localhost'] ) ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Auto-Block', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-shield',
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'traffic_monitor_auto_block_enabled', 'label' => __( 'Auto-block high-severity threats', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['traffic_monitor_auto_block_enabled'] ) ],
                        [ 'type' => 'number', 'name' => 'traffic_monitor_auto_block_threshold', 'label' => __( 'Auto-block threshold per hour', 'ultraguard-security-suite' ), 'value' => (int) ( $settings['traffic_monitor_auto_block_threshold'] ?? 5 ), 'min' => 1, 'max' => 100 ],
                        [
                            'type'     => 'select',
                            'multiple' => true,
                            'size'     => 6,
                            'name'     => 'traffic_monitor_auto_block_types',
                            'label'    => __( 'Threat types to auto-block', 'ultraguard-security-suite' ),
                            'value'    => (array) ( $settings['traffic_monitor_auto_block_types'] ?? [ 'sqli', 'xss', 'scanner', 'brute_force' ] ),
                            'options'  => [
                                [ 'value' => 'brute_force', 'label' => __( 'Brute Force', 'ultraguard-security-suite' ) ],
                                [ 'value' => 'sqli', 'label' => __( 'SQL Injection', 'ultraguard-security-suite' ) ],
                                [ 'value' => 'xss', 'label' => __( 'XSS', 'ultraguard-security-suite' ) ],
                                [ 'value' => 'scanner', 'label' => __( 'Scanner', 'ultraguard-security-suite' ) ],
                                [ 'value' => 'spam', 'label' => __( 'Spam', 'ultraguard-security-suite' ) ],
                                [ 'value' => 'clean', 'label' => __( 'Clean', 'ultraguard-security-suite' ) ],
                            ],
                        ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Ignore Rules', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-filter',
                    'data'  => [
                        [ 'type' => 'textarea', 'name' => 'traffic_monitor_ignore_user_agents', 'label' => __( 'Ignored user-agent patterns', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['traffic_monitor_ignore_user_agents'] ?? '' ) ],
                        [ 'type' => 'textarea', 'name' => 'traffic_monitor_ignore_uri_patterns', 'label' => __( 'Ignored URI patterns', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['traffic_monitor_ignore_uri_patterns'] ?? '' ) ],
                    ],
                ],
            ],
            'form'      => [
                'enable' => true,
                'id'     => 'traffic-monitor-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_save_traffic_monitor_settings',
                'nonce'  => $this->create_nonce( 'ultraguard_admin_nonce' ),
                'submit' => [
                    'text'         => __( 'Save Settings', 'ultraguard-security-suite' ),
                    'button_style' => 'primary',
                    'icon'         => 'dashicons-saved',
                ],
            ],
        ];
    }
}
