<?php
/**
 * Scanner Status Model
 * 
 * Calculates scanner-related status percentages
 * 
 * @since 2.1.0
 * @package VMPFence
 * @subpackage StatusModal
 */

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

class VMPFence_Scanner_Status {
    
    /**
     * Scan option weights for calculation
     */
    private static $option_weights = array(
        'spamvertize_check' => 0.05,
        'spam_ip_check' => 0.05,
        'gsb_check' => 0.05,
        'check_how_get_ips' => 0.05,
        'check_readable_config' => 0.05,
        'suspected_files' => 0.05,
        'core_scan' => 0.05,
        'core_unknown' => 0.05,
        'malware_scan' => 0.05,
        'file_contents' => 0.10,
        'file_contents_gsb' => 0.05,
        'posts_scan' => 0.05,
        'comments_scan' => 0.05,
        'suspicious_options' => 0.05,
        'old_versions' => 0.10,
        'suspicious_admin_users' => 0.05,
        'password_strength' => 0.05,
        'disk_space' => 0.05,
    );
    
    /**
     * Get scan type status
     * 
     * @return array Status data
     */
    public static function get_scan_type_status() {
        // Use VMPFence_Scan_Config to get scan options
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-vmpfence-scan-config.php';
        $scan_type = VMPFence_Config::get('scan_type', 'standard');
        $scan_options = VMPFence_Scan_Config::getScanOptions($scan_type);
        
        // Check if premium using VMPFence_License
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-license.php';
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-constants.php';
        $is_premium = (VMPFence_License::get_license_type() === VMPFence_Constants::LICENSE_PREMIUM);
        
        $score = 0.0;
        $disabled_count = 0;
        
        foreach (self::$option_weights as $option => $weight) {
            // Skip premium-only options for free users
            if (!$is_premium && in_array($option, array('spamvertize_check', 'spam_ip_check', 'gsb_check'))) {
                continue;
            }
            
            if (!empty($scan_options[$option])) {
                $score += $weight;
            } else {
                $disabled_count++;
            }
        }
        
        // Adjust for free users (max 70%)
        if (!$is_premium) {
            $score *= 0.70;
        }
        
        $missing = array();
        if (!$is_premium) {
            $missing[] = array(
                'percentage' => 0.30,
                'title' => 'Enable Premium Scan Signatures.'
            );
        }
        
        if ($disabled_count > 0) {
            $remaining = 1.0 - $score - ($is_premium ? 0 : 0.30);
            if ($remaining > 0) {
                $missing[] = array(
                    'percentage' => $remaining,
                    'title' => sprintf('Enable %d scan options.', $disabled_count)
                );
            }
        }
        
        return array(
            'percentage' => $score,
            'missing' => $missing,
            'title' => 'Scan Status',
            'helpLink' => admin_url('admin.php?page=vmpfence-scan-options')
        );
    }
    
    /**
     * Get scan type card status (for scan page cards)
     * Formula: 30% Premium Signatures + 11% Premium Reputation (3 checks) + 59% Scan Options (15 options)
     * 
     * @return array Status data
     */
    public static function get_scan_type_card_status() {
        // Use VMPFence_Scan_Config to get scan options
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-vmpfence-scan-config.php';
        $scan_type = VMPFence_Config::get('scanType', 'standard');
        $scan_options = VMPFence_Scan_Config::getScanOptions($scan_type);
        
        // Check if premium using VMPFence_License
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-license.php';
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-constants.php';
        $is_premium = (VMPFence_License::get_license_type() === VMPFence_Constants::LICENSE_PREMIUM);
        
        
        $score = 0.0;
        $missing = array();
        
        // Component 1: Premium Scan Signatures (30%)
        if ($is_premium) {
            $score += 0.30;
        } else {
            $missing[] = array(
                'percentage' => 0.30,
                'title' => 'Enable Premium Scan Signatures.'
            );
        }
        
        // Component 2: Premium Reputation Checks (11% total: 3.67% each for 3 checks)
        $reputation_checks = array('spamvertizeCheck', 'checkSpamIP', 'scansEnabled_checkGSB');
        $reputation_disabled = 0;
        
        if ($is_premium) {
            foreach ($reputation_checks as $check) {
                if (!empty($scan_options[$check])) {
                    $score += 0.0367;
                } else {
                    $reputation_disabled++;
                }
            }
            
            // Adjust for rounding (0.0367 * 3 = 0.1101, need exactly 0.11)
            if ($reputation_disabled == 0) {
                $score -= 0.0001;
            }
            
            if ($reputation_disabled > 0) {
                $missing[] = array(
                    'percentage' => $reputation_disabled * 0.0367,
                    'title' => sprintf('Enable %d Premium Reputation Check%s.', $reputation_disabled, $reputation_disabled > 1 ? 's' : '')
                );
            }
        } else {
            // Free users need premium for reputation checks
            $missing[] = array(
                'percentage' => 0.11,
                'title' => sprintf('Enable %d Premium Reputation Checks.', 3)
            );
        }
        
        // Component 3: Regular Scan Options (59% total: 3.93% each for 15 options)
        $regular_options = array(
            'scansEnabled_checkHowGetIPs',
            'scansEnabled_checkReadableConfig',
            'scansEnabled_suspectedFiles',
            'scansEnabled_core',
            'scansEnabled_coreUnknown',
            'scansEnabled_malware',
            'scansEnabled_fileContents',
            'scansEnabled_fileContentsGSB',
            'scansEnabled_posts',
            'scansEnabled_comments',
            'scansEnabled_suspiciousOptions',
            'scansEnabled_oldVersions',
            'scansEnabled_suspiciousAdminUsers',
            'scansEnabled_passwds',
            'scansEnabled_diskSpace'
        );
        
        $options_disabled = 0;
        foreach ($regular_options as $option) {
            if (!empty($scan_options[$option])) {
                $score += 0.0393;
            } else {
                $options_disabled++;
            }
        }
        
        // Adjust for rounding (0.0393 * 15 = 0.5895, need exactly 0.59)
        if ($options_disabled == 0) {
            $score += 0.0005;
        }
        
        if ($options_disabled > 0) {
            $missing[] = array(
                'percentage' => $options_disabled * 0.0393,
                'title' => sprintf('Enable %d scan options.', $options_disabled)
            );
        }
        
        return array(
            'percentage' => $score,
            'missing' => $missing,
            'title' => 'Scan Type Status',
            'helpLink' => admin_url('admin.php?page=vmpfence-scan-options')
        );
    }
    
    /**
     * Get reputation check status
     * 
     * @return array Status data
     */
    public static function get_reputation_status() {
        // Use VMPFence_Config for reputation checks
        $spamvertize = (bool) VMPFence_Config::get('spamvertizeCheck', 0);
        $spam_ip = (bool) VMPFence_Config::get('checkSpamIP', 0);
        $gsb = (bool) VMPFence_Config::get('scansEnabled_checkGSB', 0);
        
        $percentage = 0.0;
        if ($spamvertize) $percentage += 0.33;
        if ($spam_ip) $percentage += 0.33;
        if ($gsb) $percentage += 0.33;
        
        // Round 0.99 to 1.00 (when all three checks are enabled)
        if ($percentage == 0.99) {
            $percentage = 1.0;
        }
        
        $missing = array();
        if (!$spamvertize) {
            $missing[] = array(
                'percentage' => 0.33,
                'title' => 'Enable scan option to check if this website is being "Spamvertised".'
            );
        }
        if (!$spam_ip) {
            $missing[] = array(
                'percentage' => 0.33,
                'title' => 'Enable scan option to check if your website IP is generating spam.'
            );
        }
        if (!$gsb) {
            $missing[] = array(
                'percentage' => 0.33,
                'title' => 'Enable scan option to check if your website is on a domain blocklist.'
            );
        }
        
        return array(
            'percentage' => $percentage,
            'missing' => $missing,
            'title' => 'Reputation Check Status',
            'helpLink' => admin_url('admin.php?page=vmpfence-scan-options#general-content')
        );
    }
    
    /**
     * Get malware signatures status
     * Formula: 70% free + 30% premium = 100%
     * 
     * @return array Status data with percentage, missing items, title, and help link
     */
    public static function get_malware_signatures_status() {
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-license.php';
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-constants.php';
        
        $license_type = VMPFence_License::get_license_type();
        $is_premium = ($license_type === VMPFence_Constants::LICENSE_PREMIUM);
        
        $percentage = 0.70; // Base 70% for free users
        $missing = array();
        
        if (!$is_premium) {
            $missing[] = array(
                'percentage' => 0.30,
                'title' => 'Upgrade to Premium to enable real-time malware signatures.'
            );
        } else {
            $percentage = 1.0; // 100% for premium users
        }
        
        return array(
            'percentage' => $percentage,
            'missing' => $missing,
            'title' => 'Malware Signatures Status',
            'helpLink' => admin_url('admin.php?page=vmpfence-scan')
        );
    }
}
