<?php
/**
 * VMPFence Scanner View
 *
 * @package VMPFence
 * @since 1.0.0
 * @since 2.0.0 Updated to reflect VMP WordPress Security branding.
 * @since 2.2.2 Updated to reflect VMP Security branding.
 * @since 2.2.4 Get signature sync status for display and sync button control.
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Get license information for sync button display
$license = new VMPFence_License();
$is_premium = $license->is_premium();
$has_api = !empty(VMPFence_Config::get('central_api_key', ''));

// Get signature sync status (lightweight — reads ~100 byte meta file, no full 26 MB load)
require_once VMPFENCE_PLUGIN_DIR . 'includes/class-signature-loader.php';
$sync_metadata = VMPFence_Signature_Loader::get_sync_metadata();
$last_sync_time = $sync_metadata['last_sync'];
$last_sync_human = ($last_sync_time > 0) ? human_time_diff($last_sync_time, time()) . ' ago' : 'Never';
$sync_status = array(
    'version'         => get_option('vmpfence_signature_version', 'Unknown'),
    'last_sync'       => $last_sync_time,
    'last_sync_human' => $last_sync_human,
    'signature_count' => $sync_metadata['count'],
    'has_premium'     => $sync_metadata['has_premium'],
);
?>

<div class="wrap" style="max-width: 1300px;">
    <h1>VMP Security Malware Scanner</h1>
    
    <div class="vmpfence-scanner">
        <div class="vmpfence-card">
            <h2>Scan Status</h2>
            <p>Last scan completed: <?php echo esc_html( gmdate('Y-m-d H:i:s') ); ?></p>
            <p>Status: <span class="vmpfence-status-clean">Clean</span></p>
            
            <div class="vmpfence-scan-actions">
                <button type="button" class="button button-primary" id="vmpfence-start-scan">
                    Start New Scan
                </button>
                
                <?php if ($is_premium && $has_api): ?>
                    <button type="button" class="button" id="vmpfence-sync-signatures" style="margin-left: 10px;">
                        <span class="dashicons dashicons-update" style="margin-top: 3px;"></span>
                        Sync Signatures from API
                    </button>
                <?php elseif (!$is_premium): ?>
                    <span class="vmpfence-premium-notice" style="margin-left: 15px; color: #666;">
                        ⭐ Premium: Sync 24,903 signatures from API
                    </span>
                <?php endif; ?>
            </div>
            
            <?php if ($has_api): ?>
            <div class="vmpfence-signature-info" style="margin-top: 15px; padding: 10px; background: #f0f0f1; border-radius: 4px;">
                <strong>Signature Database:</strong>
                <div style="margin-top: 5px;">
                    <span>Version: <?php echo esc_html($sync_status['version']); ?></span> |
                    <span>Count: <?php echo esc_html(number_format($sync_status['signature_count'])); ?></span> |
                    <span>Last Sync: <?php echo esc_html($sync_status['last_sync_human']); ?></span>
                </div>
            </div>
            <?php endif; ?>
        </div>
        
        <div class="vmpfence-card">
            <h2>Scan Results</h2>
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Files Scanned</th>
                        <th>Threats Found</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><?php echo esc_html( gmdate('Y-m-d H:i:s') ); ?></td>
                        <td>1,234</td>
                        <td>0</td>
                        <td><span class="vmpfence-status-clean">Clean</span></td>
                    </tr>
                    <tr>
                        <td><?php echo esc_html( gmdate('Y-m-d H:i:s', strtotime('-1 day')) ); ?></td>
                        <td>1,232</td>
                        <td>0</td>
                        <td><span class="vmpfence-status-clean">Clean</span></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<style>
.vmpfence-card {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 20px;
    margin-bottom: 20px;
}

.vmpfence-status-clean {
    background-color: #46b450;
    color: white;
    padding: 2px 8px;
    border-radius: 3px;
    font-size: 12px;
}

.vmpfence-status-threat {
    background-color: #dc3232;
    color: white;
    padding: 2px 8px;
    border-radius: 3px;
    font-size: 12px;
}

.vmpfence-scan-actions {
    margin-top: 15px;
}

.vmpfence-premium-notice {
    font-style: italic;
    font-size: 13px;
}
</style>

<script type="text/javascript">
jQuery(document).ready(function($) {
    // Signature sync button handler
    $('#vmpfence-sync-signatures').on('click', function(e) {
        e.preventDefault();
        
        var button = $(this);
        var originalText = button.html();
        
        if (!confirm('Sync malware signatures from the API server?\n\nThis will fetch the latest signature database (may take a few moments for large updates).')) {
            return;
        }
        
        // Disable button and show loading state
        button.prop('disabled', true).html('<span class="dashicons dashicons-update spin"></span> Syncing...');
        
        // Make AJAX request
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                action: 'vmpfence_sync_signatures',
                nonce: '<?php echo wp_create_nonce('vmpfence_scan_nonce'); ?>'
            },
            success: function(response) {
                if (response.success) {
                    // Show success message
                    button.after('<span class="vmpfence-sync-success" style="color: green; margin-left: 10px;">✓ ' + response.data.message + '</span>');
                    
                    // Reload page after 2 seconds to show updated stats
                    setTimeout(function() {
                        location.reload();
                    }, 2000);
                } else {
                    // Show error message
                    alert('Signature sync failed: ' + (response.data.message || 'Unknown error'));
                    button.prop('disabled', false).html(originalText);
                }
            },
            error: function(xhr, status, error) {
                alert('AJAX error: ' + error);
                button.prop('disabled', false).html(originalText);
            }
        });
    });
});
</script>

<style>
@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}
.dashicons.spin {
    animation: spin 1s linear infinite;
}
</style>
<?php
// Enqueue admin styles (includes scanner status styles)
wp_enqueue_style(
    'vmpfence-admin',
    plugin_dir_url(dirname(__FILE__)) . 'assets/css/admin.css',
    array(),
    VMPFENCE_VERSION
);
?>
