<?php

namespace UltraguardPro\modules\api_security\pages;

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

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

class ApiSecuritySettings 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( 'api_security' );

        return [
            'type'      => 'page',
            'id'        => 'api-security-settings',
            'page_slug' => 'api-security-settings',
            'header'    => [
                'title'    => __( 'API Security Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Configure REST API access, managed API keys, XML-RPC rules, and API logging retention.', 'ultraguard-security-suite' ),
                'actions'  => [
                    $this->create_url_button( __( 'Back to API Security', 'ultraguard-security-suite' ), $this->get_page_url( 'api-security' ), 'secondary', 'dashicons-arrow-left-alt' ),
                ],
            ],
            'data'      => [
                [
                    'type'  => 'card',
                    'title' => __( 'REST API Access', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'api_security_enabled', 'label' => __( 'Enable REST API security', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['api_security_enabled'] ) ],
                        [ 'type' => 'select', 'name' => 'api_security_rest_mode', 'label' => __( 'REST API access', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['api_security_rest_mode'] ?? 'public' ), 'options' => [
                            [ 'value' => 'public', 'label' => __( 'Public as normal', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'auth_required', 'label' => __( 'Require authentication for all endpoints', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'per_route', 'label' => __( 'Use per-route rules', 'ultraguard-security-suite' ) ],
                        ] ],
                        [ 'type' => 'textarea', 'name' => 'api_security_blocked_routes', 'label' => __( 'Blocked REST API routes', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['api_security_blocked_routes'] ?? '' ) ],
                        [ 'type' => 'textarea', 'name' => 'api_security_allowed_routes', 'label' => __( 'Always-allowed routes', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['api_security_allowed_routes'] ?? '' ) ],
                        [ 'type' => 'toggle', 'name' => 'api_security_enable_api_keys', 'label' => __( 'Enable API key authentication', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['api_security_enable_api_keys'] ) ],
                        [ 'type' => 'number', 'name' => 'api_security_default_rate_limit', 'label' => __( 'Default rate limit (requests per minute)', 'ultraguard-security-suite' ), 'value' => (int) ( $settings['api_security_default_rate_limit'] ?? 60 ), 'min' => 0 ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'XML-RPC Protection', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'api_security_enable_xmlrpc', 'label' => __( 'Enable XML-RPC protection', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['api_security_enable_xmlrpc'] ) ],
                        [ 'type' => 'select', 'name' => 'api_security_xmlrpc_mode', 'label' => __( 'XML-RPC mode', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['api_security_xmlrpc_mode'] ?? 'allow_all' ), 'options' => [
                            [ 'value' => 'allow_all', 'label' => __( 'Allow all', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'disable', 'label' => __( 'Disable completely', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'allow_specific', 'label' => __( 'Allow only specific methods', 'ultraguard-security-suite' ) ],
                        ] ],
                        [ 'type' => 'textarea', 'name' => 'api_security_xmlrpc_methods', 'label' => __( 'Allowed XML-RPC methods', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['api_security_xmlrpc_methods'] ?? '' ) ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Logging', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'api_security_log_rest', 'label' => __( 'Log all REST API requests', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['api_security_log_rest'] ) ],
                        [ 'type' => 'toggle', 'name' => 'api_security_log_xmlrpc', 'label' => __( 'Log all XML-RPC calls', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['api_security_log_xmlrpc'] ) ],
                        [ 'type' => 'number', 'name' => 'api_security_log_retention_days', 'label' => __( 'Log retention days', 'ultraguard-security-suite' ), 'value' => (int) ( $settings['api_security_log_retention_days'] ?? 7 ), 'min' => 1, 'max' => 90 ],
                    ],
                ],
            ],
            'form'      => [
                'enable' => true,
                'id'     => 'api-security-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_save_api_security_settings',
                'nonce'  => $this->create_nonce( 'ultraguard_admin_nonce' ),
                'submit' => [
                    'text'         => __( 'Save Settings', 'ultraguard-security-suite' ),
                    'button_style' => 'primary',
                    'icon'         => 'dashicons-saved',
                ],
            ],
        ];
    }
}
