<?php

namespace UltraguardPro\modules\ip_intelligence\pages;

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

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

class IpIntelligenceSettings extends BasePage {

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

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

        return [
            'type'      => 'page',
            'id'        => 'ip-intelligence-settings',
            'page_slug' => 'ip-intelligence-settings',
            'header'    => [
                'title'    => __( 'IP Intelligence Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Configure feed sources, block thresholds, lookup caching, and Tor/VPN policies.', 'ultraguard-security-suite' ),
                'actions'  => [
                    $this->create_url_button( __( 'Back to Dashboard', 'ultraguard-security-suite' ), $this->get_page_url( 'ip-intelligence' ), 'secondary', 'dashicons-arrow-left-alt' ),
                ],
            ],
            'data'      => [
                [ 'type' => 'card', 'title' => __( 'Sources', 'ultraguard-security-suite' ), 'icon' => 'dashicons-cloud', 'data' => [
                    [ 'type' => 'toggle', 'name' => 'ip_intelligence_enabled', 'label' => __( 'Enable IP Intelligence module', 'ultraguard-security-suite' ), 'value' => ! empty( $s['ip_intelligence_enabled'] ) ],
                    [ 'type' => 'text', 'name' => 'ip_intelligence_abuseipdb_api_key', 'label' => __( 'AbuseIPDB API Key', 'ultraguard-security-suite' ), 'value' => (string) ( $s['ip_intelligence_abuseipdb_api_key'] ?? '' ) ],
                    [ 'type' => 'toggle', 'name' => 'ip_intelligence_enable_spamhaus', 'label' => __( 'Enable Spamhaus ZEN feed', 'ultraguard-security-suite' ), 'value' => ! empty( $s['ip_intelligence_enable_spamhaus'] ) ],
                    [ 'type' => 'toggle', 'name' => 'ip_intelligence_enable_ultraguard_network', 'label' => __( 'Enable UltraGuard Network feed', 'ultraguard-security-suite' ), 'value' => ! empty( $s['ip_intelligence_enable_ultraguard_network'] ) ],
                ] ],
                [ 'type' => 'card', 'title' => __( 'Blocking', 'ultraguard-security-suite' ), 'icon' => 'dashicons-shield', 'data' => [
                    [ 'type' => 'number', 'name' => 'ip_intelligence_abuse_score_threshold', 'label' => __( 'Abuse score threshold', 'ultraguard-security-suite' ), 'value' => (int) ( $s['ip_intelligence_abuse_score_threshold'] ?? 75 ), 'min' => 0, 'max' => 100 ],
                    [ 'type' => 'select', 'name' => 'ip_intelligence_auto_block_action', 'label' => __( 'Auto-block action', 'ultraguard-security-suite' ), 'value' => (string) ( $s['ip_intelligence_auto_block_action'] ?? 'block_and_log' ), 'options' => [
                        [ 'value' => 'block_silently', 'label' => __( 'Block silently', 'ultraguard-security-suite' ) ],
                        [ 'value' => 'block_and_log', 'label' => __( 'Block and log', 'ultraguard-security-suite' ) ],
                        [ 'value' => 'challenge_captcha', 'label' => __( 'Challenge with CAPTCHA', 'ultraguard-security-suite' ) ],
                    ] ],
                    [ 'type' => 'toggle', 'name' => 'ip_intelligence_alert_on_high_confidence', 'label' => __( 'Send alert for new high-confidence threats', 'ultraguard-security-suite' ), 'value' => ! empty( $s['ip_intelligence_alert_on_high_confidence'] ) ],
                    [ 'type' => 'toggle', 'name' => 'ip_intelligence_block_tor', 'label' => __( 'Block Tor exit nodes', 'ultraguard-security-suite' ), 'value' => ! empty( $s['ip_intelligence_block_tor'] ) ],
                    [ 'type' => 'toggle', 'name' => 'ip_intelligence_block_vpn', 'label' => __( 'Block known VPN ranges', 'ultraguard-security-suite' ), 'value' => ! empty( $s['ip_intelligence_block_vpn'] ) ],
                ] ],
                [ 'type' => 'card', 'title' => __( 'Sync & Cache', 'ultraguard-security-suite' ), 'icon' => 'dashicons-update', 'data' => [
                    [ 'type' => 'select', 'name' => 'ip_intelligence_sync_interval_hours', 'label' => __( 'Feed sync interval', 'ultraguard-security-suite' ), 'value' => (string) ( $s['ip_intelligence_sync_interval_hours'] ?? '6' ), 'options' => [
                        [ 'value' => '1', 'label' => '1' ], [ 'value' => '3', 'label' => '3' ], [ 'value' => '6', 'label' => '6' ], [ 'value' => '12', 'label' => '12' ], [ 'value' => '24', 'label' => '24' ],
                    ] ],
                    [ 'type' => 'toggle', 'name' => 'ip_intelligence_cache_lookups', 'label' => __( 'Cache individual lookups', 'ultraguard-security-suite' ), 'value' => ! empty( $s['ip_intelligence_cache_lookups'] ) ],
                    [ 'type' => 'number', 'name' => 'ip_intelligence_cache_duration_hours', 'label' => __( 'Cache duration hours', 'ultraguard-security-suite' ), 'value' => (int) ( $s['ip_intelligence_cache_duration_hours'] ?? 24 ), 'min' => 1, 'max' => 168 ],
                ] ],
            ],
            'form'      => [
                'enable' => true,
                'id'     => 'ip-intelligence-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_save_ip_intelligence_settings',
                'nonce'  => $this->create_nonce( 'ultraguard_admin_nonce' ),
                'submit' => [ 'text' => __( 'Save Settings', 'ultraguard-security-suite' ), 'button_style' => 'primary', 'icon' => 'dashicons-saved' ],
            ],
        ];
    }
}
