<?php
/**
 * Status Calculator Controller
 * 
 * Handles calculation logic for different status types
 * 
 * @since 2.1.0
 * @package VMPFence
 * @subpackage StatusModal
 */

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

class VMPFence_Status_Calculator {
    
    /**
     * Get status data for a specific type
     * 
     * @param string $status_type The status type identifier
     * @return array Status data with percentage, missing items, title, and help link
     */
    public static function get_status_data($status_type) {
        $method = 'calculate_' . $status_type;
        
        if (method_exists(__CLASS__, $method)) {
            return self::$method();
        }
        
        return array(
            'percentage' => 0,
            'missing' => array(),
            'title' => 'Unknown Status',
            'helpLink' => '#'
        );
    }
    
    /**
     * Calculate overall firewall status
     */
    private static function calculate_firewall_overall() {
        $waf_status = VMPFence_Firewall_Status::get_waf_status();
        $brute_force_status = VMPFence_Firewall_Status::get_brute_force_status();
        
        // Overall formula: WAF (80%) + Brute Force (20%)
        $percentage = ($waf_status['percentage'] * 0.80) + ($brute_force_status['percentage'] * 0.20);
        
        $missing = array();
        
        // Add detailed WAF missing items (each weighted by 80%)
        if (!empty($waf_status['missing'])) {
            foreach ($waf_status['missing'] as $item) {
                $missing[] = array(
                    'percentage' => $item['percentage'] * 0.80,
                    'title' => $item['title']
                );
            }
        }
        
        // Add detailed Brute Force missing items (each weighted by 20%)
        if (!empty($brute_force_status['missing'])) {
            foreach ($brute_force_status['missing'] as $item) {
                $missing[] = array(
                    'percentage' => $item['percentage'] * 0.20,
                    'title' => $item['title']
                );
            }
        }
        
        return array(
            'percentage' => $percentage,
            'missing' => $missing,
            'title' => 'Firewall Status',
            'helpLink' => admin_url('admin.php?page=vmpfence-firewall')
        );
    }
    
    /**
     * Calculate WAF status
     */
    private static function calculate_firewall_waf() {
        return VMPFence_Firewall_Status::get_waf_status();
    }
    
    /**
     * Calculate brute force status
     */
    private static function calculate_firewall_brute_force() {
        return VMPFence_Firewall_Status::get_brute_force_status();
    }
    
    /**
     * Calculate firewall rules status
     */
    private static function calculate_firewall_rules() {
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-license.php';
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-constants.php';
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-config.php';
        
        $is_premium = (VMPFence_License::get_license_type() === VMPFence_Constants::LICENSE_PREMIUM);
        $firewall_enabled = VMPFence_Config::get('waf_enabled', 0);
        
        $missing = array();
        $percentage = 0.0;
        
        if (!$firewall_enabled) {
            // Firewall is disabled
            $percentage = 0.0;
            $missing[] = array(
                'percentage' => 1.0,
                'title' => 'Enable Web Application Firewall.'
            );
        } elseif ($is_premium) {
            // Premium user with firewall enabled: 100%
            $percentage = 1.0;
        } else {
            // Free user with firewall enabled: 70%
            $percentage = 0.70;
            $missing[] = array(
                'percentage' => 0.30,
                'title' => 'Upgrade to Premium for real-time rule updates.'
            );
        }
        
        return array(
            'percentage' => $percentage,
            'missing' => $missing,
            'title' => $is_premium ? 'Firewall Rules: Premium' : 'Firewall Rules: Community',
            'helpLink' => admin_url('admin.php?page=vmpfence-firewall&view=options#waf-rules')
        );
    }
    
    /**
     * Calculate scan type status
     */
    private static function calculate_scan_type() {
        return VMPFence_Scanner_Status::get_scan_type_status();
    }
    
      /**
     * Calculate scan type page status
     */
    private static function calculate_scan_type_page() {
        return VMPFence_Scanner_Status::get_scan_type_card_status();
    }
    
    /**
     * Calculate reputation status
     */
    private static function calculate_scan_reputation() {
        return VMPFence_Scanner_Status::get_reputation_status();
    }
    
    /**
     * Calculate reputation checks status (alias for scan_reputation)
     */
    private static function calculate_reputation_checks() {
        return VMPFence_Scanner_Status::get_reputation_status();
    }
    
    /**
     * Calculate malware signatures status
     * Formula: 70% free + 30% premium = 100%
     */
    private static function calculate_malware_signatures() {
        require_once VMPFENCE_PLUGIN_DIR . 'status-modal/models/class-scanner-status.php';
        return VMPFence_Scanner_Status::get_malware_signatures_status();
    }
    
    /**
     * Calculate real-time IP blocklist status
     */
    private static function calculate_realtime_blocklist() {
        require_once VMPFENCE_PLUGIN_DIR . 'lib/class-config.php';
        
        $enabled = VMPFence_Config::get('realtime_ip_blocklist_enabled', 0);
        $percentage = $enabled ? 1.0 : 0.0;
    
        $missing = array();
        if (!$enabled) {
            $missing[] = array(
                'percentage' => 1.0,
                'title' => 'Enable Real-Time IP Blocklist.'
            );
        }
        
        return array(
            'percentage' => $percentage,
            'missing' => $missing,
            'title' => 'Real-Time IP Blocklist',
            'helpLink' => admin_url('admin.php?page=vmpfence-firewall&view=options')
        );
    }
    
    /**
     * Calculate overall scan status
     */
    private static function calculate_scan_overall() {
        // Check if premium
        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);
        
        // 18 scan option keys
        $scan_options = array(
            'scansEnabled_checkGSB',
            'spamvertizeCheck',
            'checkSpamIP',
            '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'
        );
        
        // Count enabled options
        $enabled_count = 0;
        foreach ($scan_options as $option) {
            if (VMPFence_Config::get($option, 0)) {
                $enabled_count++;
            }
        }
        
        $total_options = count($scan_options);
        $percentage = 0.0;
        $missing = array();
        
        if ($is_premium) {
            // Premium: 100% based on 18 scan options
            $percentage = $enabled_count / $total_options;
            
            $disabled_count = $total_options - $enabled_count;
            if ($disabled_count > 0) {
                $missing[] = array(
                    'percentage' => $disabled_count / $total_options,
                    'title' => sprintf('Enable %d scan options.', $disabled_count)
                );
            }
        } else {
            // Free: 30% premium signature + 70% scan options
            $scan_options_percentage = ($enabled_count / $total_options) * 0.70;
            $percentage = $scan_options_percentage;
            
            // Always missing 30% for premium
            $missing[] = array(
                'percentage' => 0.30,
                'title' => 'Enable Premium Scan Signatures.'
            );
            
            // Add missing scan options (out of the 70%)
            $disabled_count = $total_options - $enabled_count;
            if ($disabled_count > 0) {
                $missing[] = array(
                    'percentage' => ($disabled_count / $total_options) * 0.70,
                    'title' => sprintf('Enable %d scan options.', $disabled_count)
                );
            }
        }
        
        return array(
            'percentage' => $percentage,
            'missing' => $missing,
            'title' => 'Scan Status',
            'helpLink' => admin_url('admin.php?page=vmpfence-scan')
        );
    }
}
