<?php
/**
 * FirewallRulesAdd — Add / edit a custom firewall rule.
 *
 * @package UltraguardPro\modules\firewall\pages
 */

namespace UltraguardPro\modules\firewall\pages;

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

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

class FirewallRulesAdd extends BasePage {

    public function __construct( Plugin $plugin ) {
        parent::__construct( $plugin );
    }

    public function get_scheme( $context ): array {
        $nonce = $this->create_nonce( 'ultraguard_admin_nonce' );
        $draft = get_transient( 'ultraguard_rule_draft_' . get_current_user_id() );

        // 18 production-ready templates
        $templates = [
            [
                'text' => __( 'Block XML-RPC',           'ultraguard-security-suite' ),
                'icon' => 'dashicons-filter',
                'desc' => __( 'Block access to xmlrpc.php (common brute-force vector)', 'ultraguard-security-suite' ),
                'key'  => 'block_xmlrpc',
                'badge'=> 'danger',
            ],
            [
                'text' => __( 'Log wp-admin Access',     'ultraguard-security-suite' ),
                'icon' => 'dashicons-admin-settings',
                'desc' => __( 'Log all requests to /wp-admin for auditing', 'ultraguard-security-suite' ),
                'key'  => 'block_wp_admin',
                'badge'=> 'info',
            ],
            [
                'text' => __( 'Block PHP in Uploads',    'ultraguard-security-suite' ),
                'icon' => 'dashicons-upload',
                'desc' => __( 'Prevent execution of PHP files in uploads directory', 'ultraguard-security-suite' ),
                'key'  => 'block_php_upload',
                'badge'=> 'danger',
            ],
            [
                'text' => __( 'Block SQL Injection',     'ultraguard-security-suite' ),
                'icon' => 'dashicons-database',
                'desc' => __( 'Block UNION SELECT and common SQL injection patterns', 'ultraguard-security-suite' ),
                'key'  => 'block_sql',
                'badge'=> 'danger',
            ],
            [
                'text' => __( 'Block User Enumeration',  'ultraguard-security-suite' ),
                'icon' => 'dashicons-admin-users',
                'desc' => __( 'Block ?author=N requests used to enumerate usernames', 'ultraguard-security-suite' ),
                'key'  => 'block_user_enum',
                'badge'=> 'warning',
            ],
            [
                'text' => __( 'Block wp-config Access',  'ultraguard-security-suite' ),
                'icon' => 'dashicons-lock',
                'desc' => __( 'Block direct requests to wp-config.php', 'ultraguard-security-suite' ),
                'key'  => 'block_wp_config',
                'badge'=> 'danger',
            ],
            [
                'text' => __( 'Block .env Access',       'ultraguard-security-suite' ),
                'icon' => 'dashicons-lock',
                'desc' => __( 'Block access to .env files (common in Laravel/Symfony sites)', 'ultraguard-security-suite' ),
                'key'  => 'block_dotenv',
                'badge'=> 'danger',
            ],
            [
                'text' => __( 'Block Backup Files',      'ultraguard-security-suite' ),
                'icon' => 'dashicons-archive',
                'desc' => __( 'Block .bak, .sql, .zip backup file requests', 'ultraguard-security-suite' ),
                'key'  => 'block_backups',
                'badge'=> 'warning',
            ],
            [
                'text' => __( 'Block HEAD Scanning',     'ultraguard-security-suite' ),
                'icon' => 'dashicons-search',
                'desc' => __( 'Block HEAD requests used by scanners to enumerate paths', 'ultraguard-security-suite' ),
                'key'  => 'block_head',
                'badge'=> 'info',
            ],
        ];

        $template_items = array_map( function( array $t ): array {
            return [
                'icon'  => $t['icon'],
                'text'  => $t['text'],
                'badge' => [ 'text' => $t['badge'] === 'danger' ? __( 'Critical', 'ultraguard-security-suite' ) : ( $t['badge'] === 'warning' ? __( 'Recommended', 'ultraguard-security-suite' ) : __( 'Utility', 'ultraguard-security-suite' ) ), 'badge_type' => $t['badge'] ],
                'actions' => [
                    $this->create_ajax_button( __( 'Use', 'ultraguard-security-suite' ), 'ultraguard_use_template', [ 'template' => $t['key'] ], 'secondary' ),
                ],
            ];
        }, $templates );

        return apply_filters( 'ultraguard_firewall_rules_add_scheme', [
            'type'      => 'page',
            'id'        => 'ultraguard-firewall-add-rule',
            'page_slug' => 'firewall-add-rules',
            'class'     => 'ultraguard-firewall-add-rule-page',

            'header' => [
                'title'    => __( 'Add Firewall Rule', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Create a custom rule or apply a protection template', 'ultraguard-security-suite' ),
                'badge'    => [ 'text' => __( 'New Rule', 'ultraguard-security-suite' ), 'badge_type' => 'info' ],
                'actions'  => [
                    $this->create_url_button( __( '← Back to Rules', 'ultraguard-security-suite' ), admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=firewall-rules' ), 'secondary', 'dashicons-arrow-left-alt' ),
                ],
            ],

            'data' => [
                [
                    'type'    => 'grid',
                    'columns' => 12,
                    'gap'     => '20px',
                    'items'   => [
                        [
                            'span'      => 7,
                            'component' => [
                                'type'  => 'card',
                                'title' => __( 'Rule Configuration', 'ultraguard-security-suite' ),
                                'icon'  => 'dashicons-plus',
                                'data'  => [
                                    [ 'type' => 'text',     'name' => 'name',           'label' => __( 'Rule Name',      'ultraguard-security-suite' ), 'required' => true, 'placeholder' => __( 'e.g. Block Bad Bots', 'ultraguard-security-suite' ),       'default' => $draft['name'] ?? '' ],
                                    [ 'type' => 'textarea', 'name' => 'description',    'label' => __( 'Description',    'ultraguard-security-suite' ), 'rows' => 2,        'placeholder' => __( 'What does this rule protect against?', 'ultraguard-security-suite' ), 'default' => $draft['description'] ?? '' ],
                                    [ 'type' => 'select',   'name' => 'match_field',    'label' => __( 'Match Field',    'ultraguard-security-suite' ),
                                      'options' => [
                                          'ip'     => __( 'IP Address',    'ultraguard-security-suite' ),
                                          'uri'    => __( 'Request URI',   'ultraguard-security-suite' ),
                                          'ua'     => __( 'User-Agent',    'ultraguard-security-suite' ),
                                          'method' => __( 'HTTP Method',   'ultraguard-security-suite' ),
                                          'host'   => __( 'HTTP Host',     'ultraguard-security-suite' ),
                                      ],
                                      'default' => $draft['match_field'] ?? 'uri',
                                    ],
                                    [ 'type' => 'select',   'name' => 'match_operator', 'label' => __( 'Match Operator', 'ultraguard-security-suite' ),
                                      'options' => [
                                          'equals'       => __( 'Equals',       'ultraguard-security-suite' ),
                                          'not_equals'   => __( 'Not Equals',   'ultraguard-security-suite' ),
                                          'contains'     => __( 'Contains',     'ultraguard-security-suite' ),
                                          'not_contains' => __( 'Not Contains', 'ultraguard-security-suite' ),
                                          'starts_with'  => __( 'Starts With',  'ultraguard-security-suite' ),
                                          'ends_with'    => __( 'Ends With',    'ultraguard-security-suite' ),
                                          'regex'        => __( 'Regex',        'ultraguard-security-suite' ),
                                      ],
                                      'default' => $draft['match_operator'] ?? 'contains',
                                    ],
                                    [ 'type' => 'text',     'name' => 'match_value',    'label' => __( 'Match Value',    'ultraguard-security-suite' ), 'required' => true, 'placeholder' => __( 'e.g. /xmlrpc.php or 1.2.3.4', 'ultraguard-security-suite' ), 'default' => $draft['match_value'] ?? '' ],
                                    [ 'type' => 'select',   'name' => 'rule_action',    'label' => __( 'Action',         'ultraguard-security-suite' ),
                                      'options' => [
                                          'block' => __( 'Block (403)',         'ultraguard-security-suite' ),
                                          'log'   => __( 'Log Only',            'ultraguard-security-suite' ),
                                          'allow' => __( 'Allow (whitelist)',   'ultraguard-security-suite' ),
                                      ],
                                      'default' => $draft['rule_action'] ?? 'block',
                                    ],
                                    [ 'type' => 'number',   'name' => 'priority',       'label' => __( 'Priority',       'ultraguard-security-suite' ), 'min' => 1, 'max' => 1000, 'description' => __( 'Lower number = higher priority (1 runs first)', 'ultraguard-security-suite' ), 'default' => $draft['priority'] ?? 100 ],
                                    [ 'type' => 'toggle',   'name' => 'is_active',      'label' => __( 'Enable Rule',    'ultraguard-security-suite' ), 'description' => __( 'Activate this rule immediately after saving', 'ultraguard-security-suite' ), 'default' => $draft['is_active'] ?? true ],
                                ],
                                'footer' => [
                                    'type'      => 'button-group',
                                    'alignment' => 'right',
                                    'buttons'   => [
                                        $this->create_ajax_button( __( 'Test Pattern',   'ultraguard-security-suite' ), 'ultraguard_test_firewall_rule',   [], 'warning',   'dashicons-yes',        '' ),
                                        $this->create_ajax_button( __( 'Preview Effect', 'ultraguard-security-suite' ), 'ultraguard_preview_rule_effect',  [], 'secondary', 'dashicons-visibility'                              ),
                                        $this->create_ajax_button( __( 'Save Draft',     'ultraguard-security-suite' ), 'ultraguard_save_rule_draft',      [], 'secondary', 'dashicons-edit'                                    ),
                                        $this->create_ajax_button( __( 'Clear Form',     'ultraguard-security-suite' ), 'ultraguard_clear_rule_form',      [], 'secondary', 'dashicons-no'                                      ),
                                        $this->create_ajax_button( __( 'Add Rule',       'ultraguard-security-suite' ), 'ultraguard_save_add_new_rule',    [], 'primary',   'dashicons-plus-alt'                                ),
                                    ],
                                ],
                            ],
                        ],
                        [
                            'span'      => 5,
                            'component' => [
                                'type'  => 'card',
                                'title' => __( 'Rule Templates', 'ultraguard-security-suite' ),
                                'icon'  => 'dashicons-welcome-learn-more',
                                'data'  => [ [
                                    'type'      => 'list',
                                    'list_type' => 'ul',
                                    'items'     => $template_items,
                                ] ],
                            ],
                        ],
                    ],
                ],
            ],

            'form' => [
                'enable'  => true,
                'id'      => 'firewall-add-rule-form',
                'ajax'    => true,
                'action'  => 'ultraguard_save_add_new_rule',
                'nonce'   => $nonce,
                'submit'  => [ 'text' => __( 'Add Firewall Rule', 'ultraguard-security-suite' ), 'button_style' => 'primary' ],
            ],
        ], $context );
    }
}
