<?php
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

function moesec_admin_page() {
    $active_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'dashboard';
    ?>
    <div class="wrap">
        <img src="<?php echo esc_url(plugin_dir_url(__FILE__)) . '../assets/moesec-logo.png'; ?>" alt="MoeSec Logo" class="moesec-logo">
        <h1><?php esc_html_e('MoeSec Security', 'moesec'); ?></h1>
        
        <h2 class="nav-tab-wrapper">
            <a href="#dashboard" class="nav-tab <?php echo $active_tab === 'dashboard' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Dashboard', 'moesec'); ?></a>
            <a href="#external-scan" class="nav-tab <?php echo $active_tab === 'external-scan' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('External Scan', 'moesec'); ?></a>
            <a href="#file-db-scan" class="nav-tab <?php echo $active_tab === 'file-db-scan' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Malware Scanner', 'moesec'); ?></a>
            <a href="#integrity-check" class="nav-tab <?php echo $active_tab === 'integrity-check' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Integrity Check', 'moesec'); ?></a>
            <a href="#hardening" class="nav-tab <?php echo $active_tab === 'hardening' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Hardening', 'moesec'); ?></a>
            <a href="#login-protection" class="nav-tab <?php echo $active_tab === 'login-protection' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Login Protection', 'moesec'); ?></a>
            <a href="#firewall" class="nav-tab <?php echo $active_tab === 'firewall' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Firewall', 'moesec'); ?></a>
            <a href="#backups" class="nav-tab <?php echo $active_tab === 'backups' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Backups', 'moesec'); ?></a>
            <a href="#settings" class="nav-tab <?php echo $active_tab === 'settings' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Settings', 'moesec'); ?></a>
            <a href="#logging" class="nav-tab <?php echo $active_tab === 'logging' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Logs', 'moesec'); ?></a>
            <a href="#signature-update" class="nav-tab <?php echo $active_tab === 'signature-update' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('Update', 'moesec'); ?></a>
            <a href="#faqs" class="nav-tab <?php echo $active_tab === 'faqs' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e('FAQs', 'moesec'); ?></a>
        </h2>
        
        <div id="dashboard" class="tab-content" <?php echo $active_tab !== 'dashboard' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_dashboard_tab(); ?>
        </div>
        
        <div id="external-scan" class="tab-content" <?php echo $active_tab !== 'external-scan' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_external_scan_tab(); ?>
        </div>
        
        <div id="file-db-scan" class="tab-content" <?php echo $active_tab !== 'file-db-scan' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_file_db_scan_tab(); ?>
        </div>
        
        <div id="integrity-check" class="tab-content" <?php echo $active_tab !== 'integrity-check' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_integrity_check_tab(); ?>
        </div>
        
        <div id="hardening" class="tab-content" <?php echo $active_tab !== 'hardening' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_hardening_tab(); ?>
        </div>
        
        <div id="login-protection" class="tab-content" <?php echo $active_tab !== 'login-protection' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_login_protection_tab(); ?>
            <?php moesec_brute_force_protection_tab(); ?>
        </div>
        
        <div id="firewall" class="tab-content" <?php echo $active_tab !== 'firewall' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_firewall_tab(); ?>
            <?php moesec_bad_bot_blocking_tab();?>
        </div>
        
        <div id="backups" class="tab-content" <?php echo $active_tab !== 'backups' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_backups_tab(); ?>
        </div>
        
        <div id="settings" class="tab-content" <?php echo $active_tab !== 'settings' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_settings_tab(); ?>
        </div>
        
        <div id="logging" class="tab-content" <?php echo $active_tab !== 'logging' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_logs_tab(); ?>
        </div>

        <div id="signature-update" class="tab-content" <?php echo $active_tab !== 'signature-update' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_signature_update_tab(); ?>
        </div>
        
        <div id="faqs" class="tab-content" <?php echo $active_tab !== 'faqs' ? 'style="display:none;"' : ''; ?>>
            <?php moesec_faqs_tab(); ?>
        </div>
    </div>
    <?php
}

function moesec_dashboard_tab() {
    $recent_logs = moesec_get_recent_logs(20);
    ?>
    <h3><?php esc_html_e('Security Dashboard', 'moesec'); ?></h3>
    
    <!-- Review Request Section -->
    <div class="moesec-review-request" style="background: #f0f8ff; border: 2px solid #0073aa; border-radius: 8px; padding: 15px; margin: 20px 0; text-align: center;">
        <h4 style="color: #0073aa; margin-top: 0;">
            <span class="dashicons dashicons-star-filled" style="color: #ffb900;"></span>
            <?php esc_html_e('Did MoeSec help secure your website?', 'moesec'); ?>
            <span class="dashicons dashicons-star-filled" style="color: #ffb900;"></span>
        </h4>
        <p style="margin: 10px 0;">
            <?php esc_html_e('If MoeSec helped you find malware, secure your site, or you simply want to support our free plugin, please consider leaving us a 5-star review!', 'moesec'); ?>
        </p>
        <a href="https://wordpress.org/support/plugin/moesec/reviews/#new-post" 
           class="button button-primary" 
           target="_blank" 
           style="background: #0073aa; border-color: #0073aa; text-shadow: none; box-shadow: none;">
            <span class="dashicons dashicons-thumbs-up" style="vertical-align: middle; margin-right: 5px;"></span>
            <?php esc_html_e('Leave a Review ⭐⭐⭐⭐⭐', 'moesec'); ?>
        </a>
        <p style="font-size: 12px; color: #666; margin: 10px 0 0 0;">
            <?php esc_html_e('Your feedback helps us improve and helps other users discover MoeSec!', 'moesec'); ?>
        </p>
    </div>
    
    <div class="moesec-dashboard-summary">
        <div class="moesec-dashboard-item">
            <h4><?php esc_html_e('Last Scan', 'moesec'); ?></h4>
            <p><?php echo esc_html(get_option('moesec_last_scan_time', __('Never', 'moesec'))); ?></p>
        </div>
        <div class="moesec-dashboard-item">
            <h4><?php esc_html_e('Threats Detected', 'moesec'); ?></h4>
            <p><?php echo esc_html(get_option('moesec_threats_detected', '0')); ?></p>
        </div>
        <div class="moesec-dashboard-item">
            <h4><?php esc_html_e('WordPress Version', 'moesec'); ?></h4>
            <p><?php echo esc_html(get_bloginfo('version')); ?></p>
        </div>
        <div class="moesec-dashboard-item">
            <h4><?php esc_html_e('Signatures Last Updated', 'moesec'); ?></h4>
            <p><?php echo esc_html(moesec_get_last_signature_update()); ?></p>
            <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=moesec&moesec_update_signatures=true'), 'moesec_update_signatures')); ?>" class="button button-secondary"><?php esc_html_e('Update Now', 'moesec'); ?></a>
        </div>
        <div class="moesec-dashboard-item">
            <h4><?php esc_html_e('Scheduled Scans', 'moesec'); ?></h4>
            <p><?php echo get_option('moesec_scheduled_scans', false) ? esc_html(get_option('moesec_scan_frequency', 'daily')) : esc_html__('Disabled', 'moesec'); ?></p>
            <?php
            $next_scan = '';
            $frequency = get_option('moesec_scan_frequency', 'daily');
            if (get_option('moesec_scheduled_scans', false)) {
                switch ($frequency) {
                    case 'daily':
                        $next_scan = wp_next_scheduled('moesec_daily_scan');
                        break;
                    case 'weekly':
                        $next_scan = wp_next_scheduled('moesec_weekly_scan');
                        break;
                    case 'monthly':
                        $next_scan = wp_next_scheduled('moesec_monthly_scan');
                        break;
                }
                if ($next_scan) {
                    echo '<p><small>' . sprintf(esc_html__('Next scan: %s', 'moesec'), date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $next_scan)) . '</small></p>';
                }
            }
            ?>
        </div>
    </div>
    <h4><?php esc_html_e('Recent Activity (Last 20 Records)', 'moesec'); ?></h4>
    <table class="widefat">
        <thead>
            <tr>
                <th><?php esc_html_e('Time', 'moesec'); ?></th>
                <th><?php esc_html_e('Event', 'moesec'); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($recent_logs as $log): ?>
            <tr>
                <td><?php echo esc_html($log->time); ?></td>
                <td><?php echo esc_html($log->message); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <table>
        <th> <h3>Need Expert Help?</h4>
            <p>For Advanced Malware Removal and Complete Security Platform that includes Advanced WAF, Backups, Daily External & Internal Scans, Blacklist Monitoring & Removal, Website Antivirus, Trust Seals and MUCH more with instant notification & UNLIMITED Support & Cleanup requests by Multi-Certified Security Professionals! </p>
            <a href="https://moesec.com/" class="button button-primary moesec-cleanup-button" target="_blank">Request Professional Cleanup</a>         
            <h3>Check our Latest Research Blogs</h4>
            <a href="https://moesec.com/blog/" class="button button-primary moesec-cleanup-button" target="_blank">MoeSec Research Blog</a>
        </th>

        <th><br><br><br><iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/m6FcDT6NMNQ?si=z5XOa7B1z8F9b5qe" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></th>
    </table>

    <a href="https://MoeSec.com"><img src="<?php echo esc_url(plugin_dir_url(__FILE__)) . '../assets/32.webp'; ?>" rel="me" alt="MoeSec" width=32 height=32></a>
    <a href="https://twitter.com/MoeSecCom"><img src="<?php echo esc_url(plugin_dir_url(__FILE__)) . '../assets/twitter.png'; ?>" rel="me" alt="Follow us on Twitter" width=32 height=32></a>
    <a href="https://www.facebook.com/MoeSecCom"><img src="<?php echo esc_url(plugin_dir_url(__FILE__)) . '../assets/facebook.png'; ?>" rel="me" alt="Follow us on FaceBook" width=32 height=32></a>
    <?php
}

function moesec_settings_tab() {
    if (isset($_POST['moesec_save_settings']) && check_admin_referer('moesec_save_settings', 'moesec_settings_nonce')) {
        update_option('moesec_email_notifications', isset($_POST['email_notifications']));
        update_option('moesec_scheduled_scans', isset($_POST['scheduled_scans']));
        update_option('moesec_scan_frequency', sanitize_text_field(wp_unslash($_POST['scan_frequency'])));
        update_option('moesec_notification_email', sanitize_email(wp_unslash($_POST['notification_email'])));
        
        // Backup settings
        update_option('moesec_enable_auto_backup', isset($_POST['enable_auto_backup']));
        update_option('moesec_backup_frequency', sanitize_text_field(wp_unslash($_POST['backup_frequency'])));
        
        // Integrity check settings
        update_option('moesec_enable_integrity_check', isset($_POST['enable_integrity_check']));
        update_option('moesec_integrity_check_frequency', sanitize_text_field(wp_unslash($_POST['integrity_check_frequency'])));
        
        moesec_setup_scheduled_scans();
        moesec_schedule_automatic_backups();
        moesec_schedule_integrity_check();
        
        moesec_log('Settings updated: All scheduled tasks have been reconfigured');
        
        echo '<div class="updated"><p>' . esc_html__('Settings saved successfully. All scheduled tasks have been updated.', 'moesec') . '</p></div>';
    }
    
    $email_notifications = get_option('moesec_email_notifications', false);
    $scheduled_scans = get_option('moesec_scheduled_scans', false);
    $scan_frequency = get_option('moesec_scan_frequency', 'daily');
    $notification_email = get_option('moesec_notification_email', get_option('admin_email'));
    $enable_auto_backup = get_option('moesec_enable_auto_backup', false);
    $backup_frequency = get_option('moesec_backup_frequency', 'weekly');
    $enable_integrity_check = get_option('moesec_enable_integrity_check', false);
    $integrity_check_frequency = get_option('moesec_integrity_check_frequency', 'daily');
    ?>
    <h3><?php esc_html_e('MoeSec Settings', 'moesec'); ?></h3>
    <form method="post" action="#settings">
        <?php wp_nonce_field('moesec_save_settings', 'moesec_settings_nonce'); ?>
        
        <h4><?php esc_html_e('Scan Settings', 'moesec'); ?></h4>
        <table class="form-table">
            <tr>
                <th><label for="email_notifications"><?php esc_html_e('Email Notifications', 'moesec'); ?></label></th>
                <td><input type="checkbox" id="email_notifications" name="email_notifications" <?php checked($email_notifications); ?>></td>
            </tr>
            <tr>
                <th><label for="scheduled_scans"><?php esc_html_e('Scheduled Scans', 'moesec'); ?></label></th>
                <td><input type="checkbox" id="scheduled_scans" name="scheduled_scans" <?php checked($scheduled_scans); ?>></td>
            </tr>
            <tr>
                <th><label for="scan_frequency"><?php esc_html_e('Scan Frequency', 'moesec'); ?></label></th>
                <td>
                    <select id="scan_frequency" name="scan_frequency">
                        <option value="daily" <?php selected($scan_frequency, 'daily'); ?>><?php esc_html_e('Daily', 'moesec'); ?></option>
                        <option value="weekly" <?php selected($scan_frequency, 'weekly'); ?>><?php esc_html_e('Weekly', 'moesec'); ?></option>
                        <option value="monthly" <?php selected($scan_frequency, 'monthly'); ?>><?php esc_html_e('Monthly', 'moesec'); ?></option>
                    </select>
                </td>
            </tr>
            <tr>
                <th><label for="notification_email"><?php esc_html_e('Notification Email', 'moesec'); ?></label></th>
                <td><input type="email" id="notification_email" name="notification_email" value="<?php echo esc_attr($notification_email); ?>"></td>
            </tr>
        </table>
        
        <h4><?php esc_html_e('Backup Settings', 'moesec'); ?></h4>
        <table class="form-table">
            <tr>
                <th><label for="enable_auto_backup"><?php esc_html_e('Enable Automatic Backups', 'moesec'); ?></label></th>
                <td>
                    <input type="checkbox" id="enable_auto_backup" name="enable_auto_backup" <?php checked($enable_auto_backup); ?>>
                    <p class="description"><?php esc_html_e('Automatically create backups on a schedule. Old backups will be cleaned up (keeps last 10).', 'moesec'); ?></p>
                </td>
            </tr>
            <tr>
                <th><label for="backup_frequency"><?php esc_html_e('Backup Frequency', 'moesec'); ?></label></th>
                <td>
                    <select id="backup_frequency" name="backup_frequency">
                        <option value="daily" <?php selected($backup_frequency, 'daily'); ?>><?php esc_html_e('Daily', 'moesec'); ?></option>
                        <option value="weekly" <?php selected($backup_frequency, 'weekly'); ?>><?php esc_html_e('Weekly', 'moesec'); ?></option>
                        <option value="monthly" <?php selected($backup_frequency, 'monthly'); ?>><?php esc_html_e('Monthly', 'moesec'); ?></option>
                    </select>
                </td>
            </tr>
        </table>
        
        <h4><?php esc_html_e('Integrity Check Settings', 'moesec'); ?></h4>
        <table class="form-table">
            <tr>
                <th><label for="enable_integrity_check"><?php esc_html_e('Enable Integrity Checks', 'moesec'); ?></label></th>
                <td>
                    <input type="checkbox" id="enable_integrity_check" name="enable_integrity_check" <?php checked($enable_integrity_check); ?>>
                    <p class="description"><?php esc_html_e('Automatically check WordPress core file integrity on a schedule.', 'moesec'); ?></p>
                </td>
            </tr>
            <tr>
                <th><label for="integrity_check_frequency"><?php esc_html_e('Integrity Check Frequency', 'moesec'); ?></label></th>
                <td>
                    <select id="integrity_check_frequency" name="integrity_check_frequency">
                        <option value="daily" <?php selected($integrity_check_frequency, 'daily'); ?>><?php esc_html_e('Daily', 'moesec'); ?></option>
                        <option value="weekly" <?php selected($integrity_check_frequency, 'weekly'); ?>><?php esc_html_e('Weekly', 'moesec'); ?></option>
                        <option value="monthly" <?php selected($integrity_check_frequency, 'monthly'); ?>><?php esc_html_e('Monthly', 'moesec'); ?></option>
                    </select>
                </td>
            </tr>
        </table>
        
        <p class="submit">
            <input type="submit" name="moesec_save_settings" class="button button-primary" value="<?php esc_attr_e('Save Settings', 'moesec'); ?>">
        </p>
    </form>
    <?php
}
?>
