<?php
if (!defined('ABSPATH')) {
    die('You are not allowed to call this page directly.');
}

// Load saved settings
$saved_options = get_option('formshield_options', array());
$admin_email = isset($saved_options['admin_email'])
    ? $saved_options['admin_email']
    : get_option('admin_email', '');
$attack_notifications = isset($saved_options['attack_notifications']) ? $saved_options['attack_notifications'] : true;
$form_detection_notifications = isset($saved_options['form_detection_notifications']) ? $saved_options['form_detection_notifications'] : true;
$periodic_reports = isset($saved_options['periodic_reports']) ? $saved_options['periodic_reports'] : false;
$report_frequency = isset($saved_options['report_frequency']) ? $saved_options['report_frequency'] : '15_days';
$protection_level = isset($saved_options['protection_level']) ? $saved_options['protection_level'] : 'medium';
$auto_protect_forms = isset($saved_options['auto_protect_forms']) ? $saved_options['auto_protect_forms'] : true;
$block_suspicious_ips = isset($saved_options['block_suspicious_ips']) ? $saved_options['block_suspicious_ips'] : true;
$rate_limiting = isset($saved_options['rate_limiting']) ? $saved_options['rate_limiting'] : false;
$debug_mode = isset($saved_options['debug_mode']) ? $saved_options['debug_mode'] : false;
$whitelist_ips = isset($saved_options['whitelist_ips']) ? $saved_options['whitelist_ips'] : '';
$whitelist_enabled = isset($saved_options['whitelist_enabled']) ? $saved_options['whitelist_enabled'] : false;
$protect_comments = isset($saved_options['protect_comments']) ? $saved_options['protect_comments'] : true;
$disable_comments_completely = isset($saved_options['disable_comments_completely']) ? $saved_options['disable_comments_completely'] : false;

// CSS се зарежда автоматично от formshield.php

// Enqueue settings page script
wp_enqueue_script( 'formshield-admin-settings', plugins_url( 'js/settings-page.js', dirname(__FILE__) ), array(), FORMSHIELD_VERSION, true );

// Localize script with AJAX data and strings
wp_localize_script( 'formshield-admin-settings', 'formshieldSettings', array(
    'ajaxUrl' => admin_url('admin-ajax.php'),
    'autoSaveNonce' => wp_create_nonce('formshield_auto_save'),
    'strings' => array(
        'settingSaved' => __('✓ Setting saved', 'formshield'),
        'pleaseEnterValidEmail' => __('Please enter a valid email address.', 'formshield'),
        'pleaseEnterValidEmailFormat' => __('Please enter a valid email address format.', 'formshield'),
        // translators: %s is the email address where the test email was sent
        'testEmailSent' => __('Test email sent to: %s\n\nCheck your inbox for the test message.', 'formshield'),
        'ipWhitelistDisabled' => __('IP Whitelist is disabled. Enable it first to save IP addresses.', 'formshield'),
        'pleaseEnterAtLeastOneIP' => __('Please enter at least one IP address.', 'formshield'),
        // translators: %s is the list of invalid IP addresses
        'invalidIPsFound' => __('Invalid IP addresses found:\n%s\n\nPlease fix them before saving.', 'formshield'),
        'ipWhitelistSaved' => __('IP Whitelist saved successfully!', 'formshield'),
        'noIPsToValidate' => __('No IP addresses to validate.', 'formshield'),
        'validationResults' => __('Validation Results:\n\n', 'formshield'),
        // translators: %1$d is the number of valid IPs, %2$s is the list of valid IP addresses
        'validIPs' => __('Valid IPs (%1$d):\n%2$s\n\n', 'formshield'),
        // translators: %1$d is the number of invalid IPs, %2$s is the list of invalid IP addresses
        'invalidIPs' => __('Invalid IPs (%1$d):\n%2$s\n\n', 'formshield'),
        'pleaseFixInvalidIPs' => __('Please fix the invalid IP addresses.', 'formshield'),
        'allIPsValid' => __('All IP addresses are valid!', 'formshield'),
        'confirmClearWhitelist' => __('Are you sure you want to clear all IP addresses from the whitelist?', 'formshield'),
        'ipWhitelistCleared' => __('IP Whitelist cleared successfully!', 'formshield')
    ),
    'savedSettings' => array(
        'admin_email' => $admin_email,
        'attack_notifications' => $attack_notifications,
        'form_detection_notifications' => $form_detection_notifications,
        'periodic_reports' => $periodic_reports,
        'protection_level' => $protection_level,
        'auto_protect_forms' => $auto_protect_forms,
        'block_suspicious_ips' => $block_suspicious_ips,
        'rate_limiting' => $rate_limiting,
        'debug_mode' => $debug_mode,
        'whitelist_ips' => $whitelist_ips,
        'whitelist_enabled' => $whitelist_enabled,
        'protect_comments' => $protect_comments,
        'disable_comments_completely' => $disable_comments_completely
    )
) );
?>

<!-- JavaScript moved to settings-page.js -->
<div class="settings">
    <header>
                <h1><?php esc_html_e('FormShield Settings', 'formshield'); ?></h1>
        <p class="subtitle"><?php esc_html_e('Manage core form protection settings. Choose how to detect new forms, how to block suspicious IP addresses, and enable diagnostic mode when needed.', 'formshield'); ?></p>
    </header>

    <!-- Section 1: Email Notifications & Advanced Security -->
    <section>
        <div class="grid-2">
            <!-- Periodic Reports -->
            <div class="settings-column">
                <h2><?php esc_html_e('Periodic Reports', 'formshield'); ?></h2>
                <div class="form-group">
                    <label class="field-label">
                        <?php esc_html_e('Admin Email', 'formshield'); ?>
                        <span class="info" data-tip="<?php esc_attr_e('Email address for receiving periodic security reports.', 'formshield'); ?>">i</span>
                    </label>
                    <input type="email" name="admin_email" value="<?php echo esc_attr($admin_email); ?>" placeholder="admin@yoursite.com"/>
                </div>
                
                <div class="row">
                    <label class="switch"><input type="checkbox" name="periodic_reports" id="periodic_reports" <?php echo $periodic_reports ? 'checked' : ''; ?>><span class="slider"></span></label>
                    <span class="inline-label"><?php esc_html_e('Enable Periodic Reports', 'formshield'); ?></span>
                    <span class="info" data-tip="<?php esc_html_e('Receive regular reports about security activity and form protection status.', 'formshield'); ?>">i</span>
                </div>

                <div class="form-group" id="report_frequency_group">
                    <label class="field-label">
                        <?php esc_html_e('Report Frequency', 'formshield'); ?>
                        <span class="info" data-tip="<?php esc_html_e('How often to receive security reports.', 'formshield'); ?>">i</span>
                    </label>
                    <select id="report_frequency" name="report_frequency">
                        <option value="15_days" <?php echo $report_frequency === '15_days' ? 'selected' : ''; ?>><?php esc_html_e('Every 15 Days', 'formshield'); ?></option>
                        <option value="30_days" <?php echo $report_frequency === '30_days' ? 'selected' : ''; ?>><?php esc_html_e('Every 30 Days', 'formshield'); ?></option>
                    </select>
                </div>

                <div class="actions">
                    <button class="btn btn-secondary" onclick="sendTestEmail()"><?php esc_html_e('Send Test Email', 'formshield'); ?></button>
                </div>
            </div>

            <!-- Advanced Security -->
            <div class="settings-column">
                <h2>Advanced Security</h2>
                <div class="form-group">
                    <label class="field-label">
                        Protection Level
                        <span class="info" data-tip="Low/Medium/High presets for bot detection sensitivity.">i</span>
                    </label>
                    <select name="protection_level">
                        <option value="low" <?php echo $protection_level === 'low' ? 'selected' : ''; ?>>Low — Permissive</option>
                        <option value="medium" <?php echo $protection_level === 'medium' ? 'selected' : ''; ?>>Medium — Balanced protection</option>
                        <option value="high" <?php echo $protection_level === 'high' ? 'selected' : ''; ?>>High — Strict</option>
                    </select>
                </div>

                <div class="row">
                    <label class="switch"><input type="checkbox" name="auto_protect_forms" <?php echo $auto_protect_forms ? 'checked' : ''; ?>><span class="slider"></span></label>
                    <span class="inline-label">Auto-protect New Forms</span>
                    <span class="info" data-tip="Newly detected forms are protected automatically.">i</span>
                </div>

                <div class="row">
                    <label class="switch"><input type="checkbox" name="block_suspicious_ips" <?php echo $block_suspicious_ips ? 'checked' : ''; ?>><span class="slider"></span></label>
                    <span class="inline-label">Block Suspicious IPs</span>
                    <span class="info" data-tip="Automatically block IPs with suspicious behavior patterns.">i</span>
            </div>

                <div class="row">
                    <label class="switch"><input type="checkbox" name="rate_limiting" <?php echo $rate_limiting ? 'checked' : ''; ?>><span class="slider"></span></label>
                    <span class="inline-label">Rate Limiting</span>
                    <span class="info" data-tip="Limit form submissions per IP address per minute.">i</span>
        </div>

                <div class="row">
                    <label class="switch"><input type="checkbox" name="debug_mode" <?php echo $debug_mode ? 'checked' : ''; ?>><span class="slider"></span></label>
                    <span class="inline-label">Debug Mode</span>
                    <span class="info" data-tip="Adds verbose logs in the Activity view for troubleshooting.">i</span>
                </div>

                <div class="row">
                    <label class="switch"><input type="checkbox" name="protect_comments" <?php echo $protect_comments ? 'checked' : ''; ?>><span class="slider"></span></label>
                    <span class="inline-label">Protect WordPress Comments</span>
                    <span class="info" data-tip="Enable spam protection for comments on posts, pages, images, and all content types that support comments.">i</span>
                </div>

                <div class="row">
                    <label class="switch"><input type="checkbox" name="disable_comments_completely" <?php echo $disable_comments_completely ? 'checked' : ''; ?>><span class="slider"></span></label>
                    <span class="inline-label">Disable Comments Completely</span>
                    <span class="info" data-tip="Completely disable comments on all posts, pages, images, and content types. This will close comments site-wide and hide comment forms.">i</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Section 2: IP Whitelist -->
    <section>
        <div class="grid-1">
            <!-- IP Whitelist Management -->
            <div class="settings-column">
                <h2>IP Whitelist Management</h2>
                <div class="form-group">
                    <label class="field-label">
                        IP Addresses
                        <span class="info" data-tip="One per line. IPv4/IPv6/CIDR supported. Examples: 192.168.1.1, 10.0.0.0/24, ::1">i</span>
                    </label>
                    <textarea id="whitelist-ips" name="whitelist-ips" placeholder="Enter IP addresses, one per line"><?php echo esc_textarea($whitelist_ips); ?></textarea>
                </div>

                <div class="row">
                    <label class="switch"><input type="checkbox" id="whitelist-enabled" name="whitelist-enabled" <?php echo $whitelist_enabled ? 'checked' : ''; ?>><span class="slider"></span></label>
                    <span class="inline-label">Enable IP Whitelist</span>
                    <span class="info" data-tip="Only listed IPs bypass protection. All others are blocked.">i</span>
                </div>

                <div class="actions">
                    <button class="btn btn-secondary" onclick="validateIPs()"><?php esc_html_e('Validate IPs', 'formshield'); ?></button>
                    <button class="btn btn-secondary" onclick="clearWhitelist()"><?php esc_html_e('Clear All', 'formshield'); ?></button>
                </div>
            </div>
        </div>
    </section>

    <!-- Footer Bar -->
    <div class="footer-bar">
        <span class="items">Settings are saved automatically when you make changes.</span>
    </div>
</div>

<!-- Auto-save functionality moved to settings-page.js -->
<?php
// Enqueue admin styles
wp_enqueue_style( 'formshield-admin-override', plugins_url( 'css/admin.css', dirname(__FILE__) ), array('formshield-admin'), FORMSHIELD_VERSION );
?>
