
<?php
class CompatibilityValidator {
    private $db;
    
    public function __construct($db) {
        $this->db = $db;
    }
    
    public function validate($components) {
        $errors = [];
        
        // Processor dan Motherboard compatibility check
        if (!empty($components['processor']) && !empty($components['motherboard'])) {
            $processor_id = $components['processor']['id'];
            $motherboard_id = $components['motherboard']['id'];
            
            // Cek socket compatibility
            $stmt = $this->db->prepare("
                SELECT p.name as proc_name, m.name as mobo_name, 
                       sp.socket_id as proc_socket, sm.socket_id as mobo_socket,
                       s1.name as proc_socket_name, s2.name as mobo_socket_name
                FROM processors p 
                JOIN processor_compatibility sp ON p.id = sp.processor_id
                CROSS JOIN motherboards m 
                JOIN motherboard_supports sm ON m.id = sm.motherboard_id
                LEFT JOIN socket_types s1 ON sp.socket_id = s1.id
                LEFT JOIN socket_types s2 ON sm.socket_id = s2.id
                WHERE p.id = ? AND m.id = ?
            ");
            $stmt->execute([$processor_id, $motherboard_id]);
            $result = $stmt->fetch();
            
            if ($result && $result['proc_socket'] !== $result['mobo_socket']) {
                $errors[] = "Processor {$result['proc_name']} (Socket {$result['proc_socket_name']}) 
                            tidak kompatibel dengan Motherboard {$result['mobo_name']} 
                            (Socket {$result['mobo_socket_name']})";
            }
        }
        
        // RAM dan Motherboard compatibility check
        if (!empty($components['ram']) && !empty($components['motherboard'])) {
            $ram_id = $components['ram']['id'];
            $motherboard_id = $components['motherboard']['id'];
            
            $stmt = $this->db->prepare("
                SELECT r.name as ram_name, m.name as mobo_name,
                       r.type as ram_type, rt.name as required_ram_type
                FROM ram r
                CROSS JOIN motherboards m 
                JOIN motherboard_supports ms ON m.id = ms.motherboard_id
                JOIN ram_types rt ON ms.ram_type_id = rt.id
                WHERE r.id = ? AND m.id = ?
            ");
            $stmt->execute([$ram_id, $motherboard_id]);
            $result = $stmt->fetch();
            
            // Convert RAM type value to DDR version
            $ramType = '';
            if ($result['ram_type'] <= 0.3) $ramType = 'DDR3';
            else if ($result['ram_type'] <= 0.7) $ramType = 'DDR4';
            else $ramType = 'DDR5';
            
            if ($ramType !== $result['required_ram_type']) {
                $errors[] = "RAM {$result['ram_name']} (Type {$ramType}) 
                            tidak kompatibel dengan Motherboard {$result['mobo_name']} 
                            (Support {$result['required_ram_type']})";
            }
        }
        
        // Storage NVMe check dengan Motherboard
        if (!empty($components['storage']) && !empty($components['motherboard'])) {
            $storage_id = $components['storage']['id'];
            $motherboard_id = $components['motherboard']['id'];
            
            $stmt = $this->db->prepare("
                SELECT s.name as storage_name, m.name as mobo_name,
                       s.type as storage_type, ms.has_nvme
                FROM storage s
                CROSS JOIN motherboards m 
                JOIN motherboard_supports ms ON m.id = ms.motherboard_id
                WHERE s.id = ? AND m.id = ?
            ");
            $stmt->execute([$storage_id, $motherboard_id]);
            $result = $stmt->fetch();
            
            // Check if NVMe SSD is used on non-NVMe motherboard
            if ($result['storage_type'] > 0.8 && !$result['has_nvme']) {
                $errors[] = "Storage NVMe {$result['storage_name']} 
                            tidak dapat digunakan pada Motherboard {$result['mobo_name']} 
                            (Tidak mendukung NVMe)";
            }
        }
        
        // VGA dan Power Supply compatibility check
        if (!empty($components['vga'])) {
            $vga = $components['vga'];
            $vga_wattage = $vga['wattage'];
            
            // Tambahkan 100W untuk kebutuhan dasar sistem
            $minimum_psu_required = $vga_wattage + 300;

            // Jika ada PSU yang dipilih
            if (!empty($components['power_supply'])) {
                $psu = $components['power_supply'];
                $psu_wattage = $psu['wattage'];

                if ($psu_wattage < $minimum_psu_required) {
                    $errors[] = "Power Supply {$psu['name']} ({$psu_wattage}W) tidak mencukupi untuk VGA {$vga['name']} 
                                yang membutuhkan setidaknya minimum {$minimum_psu_required}w demi keamanan( VGA: {$vga_wattage}w + System: 300w )";
                }
            }
        }
        
        return $errors;
    }

    // Helper function untuk get socket compatibility
    private function getSocketCompatibility($processor_id, $motherboard_id) {
        $stmt = $this->db->prepare("
            SELECT s1.name as proc_socket, s2.name as mobo_socket
            FROM processor_compatibility pc
            JOIN socket_types s1 ON pc.socket_id = s1.id
            CROSS JOIN motherboard_supports ms
            JOIN socket_types s2 ON ms.socket_id = s2.id
            WHERE pc.processor_id = ? AND ms.motherboard_id = ?
        ");
        $stmt->execute([$processor_id, $motherboard_id]);
        return $stmt->fetch();
    }

    // Helper function untuk get RAM compatibility
    private function getRAMCompatibility($ram_id, $motherboard_id) {
        $stmt = $this->db->prepare("
            SELECT r.type as ram_type, rt.name as mobo_ram_type
            FROM ram r
            CROSS JOIN motherboard_supports ms
            JOIN ram_types rt ON ms.ram_type_id = rt.id
            WHERE r.id = ? AND ms.motherboard_id = ?
        ");
        $stmt->execute([$ram_id, $motherboard_id]);
        return $stmt->fetch();
    }

    // Helper function untuk get storage compatibility
    private function getStorageCompatibility($storage_id, $motherboard_id) {
        $stmt = $this->db->prepare("
            SELECT s.type as storage_type, ms.has_nvme
            FROM storage s
            CROSS JOIN motherboard_supports ms
            WHERE s.id = ? AND ms.motherboard_id = ?
        ");
        $stmt->execute([$storage_id, $motherboard_id]);
        return $stmt->fetch();
    }

    // Helper function untuk convert RAM type value to string
    private function getRAMTypeString($type_value) {
        if ($type_value <= 0.3) return 'DDR3';
        if ($type_value <= 0.7) return 'DDR4';
        return 'DDR5';
    }

    // Helper function untuk get power requirement
    private function getPowerRequirement($vga_id) {
        $stmt = $this->db->prepare("SELECT wattage FROM vga_cards WHERE id = ?");
        $stmt->execute([$vga_id]);
        $result = $stmt->fetch();
        return $result ? $result['wattage'] : 0;
    }
}
?>


