<?php
if (!defined('ABSPATH')) {
    exit;
}

class S3_Upload_Resource_Checker {
    
    public static function check_server_health() {
        $health = array(
            'memory_limit' => ini_get('memory_limit'),
            'max_execution_time' => ini_get('max_execution_time'),
            'upload_max_filesize' => ini_get('upload_max_filesize'),
            'post_max_size' => ini_get('post_max_size'),
            'php_version' => phpversion(),
            'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown',
            'memory_usage' => round(memory_get_usage(true) / 1024 / 1024, 2) . 'MB',
            'memory_peak_usage' => round(memory_get_peak_usage(true) / 1024 / 1024, 2) . 'MB'
        );
        
        return $health;
    }
    
    public static function get_recommendations() {
        $recommendations = array();
        
        $memory_limit = ini_get('memory_limit');
        if (self::convert_to_bytes($memory_limit) < (128 * 1024 * 1024)) {
            $recommendations[] = 'Increase PHP memory_limit to at least 128M (current: ' . $memory_limit . ')';
        }
        
        $max_execution_time = ini_get('max_execution_time');
        if ($max_execution_time < 60) {
            $recommendations[] = 'Increase max_execution_time to at least 60 (current: ' . $max_execution_time . ')';
        }
        
        $upload_max_filesize = ini_get('upload_max_filesize');
        if (self::convert_to_bytes($upload_max_filesize) < (64 * 1024 * 1024)) {
            $recommendations[] = 'Increase upload_max_filesize to at least 64M (current: ' . $upload_max_filesize . ')';
        }
        
        return $recommendations;
    }
    
    public static function convert_to_bytes($size) {
        if (preg_match('/^(\d+)(.)$/', $size, $matches)) {
            if ($matches[2] == 'M') {
                return $matches[1] * 1024 * 1024;
            } elseif ($matches[2] == 'K') {
                return $matches[1] * 1024;
            } elseif ($matches[2] == 'G') {
                return $matches[1] * 1024 * 1024 * 1024;
            }
        }
        return 0;
    }
}