<?php
class FuzzyTsukamoto {
    private $db;
    
    public function __construct($db) {
        $this->db = $db;
    }

    // Processor membership functions
    private function processorPerformanceMembership($value) {
        $low = ($value <= 1) ? 1 : (($value <= 5) ? ((5 - $value) / 4) : 0);
        $medium = ($value <= 3) ? 0 : (($value <= 5) ? ((($value - 3) / 2)) : 
                 (($value <= 7) ? ((7 - $value) / 2) : 0));
        $high = ($value <= 5) ? 0 : (($value <= 10) ? (($value - 5) / 5) : 1);
        
        return ['low' => $low, 'medium' => $medium, 'high' => $high];
    }

    private function processorAgeMembership($value) {
        $new = ($value <= 0) ? 1 : (($value <= 1) ? (1 - $value) : 0);
        $old = ($value <= 1) ? 0 : (($value <= 3) ? (($value - 1) / 2) : 
               (($value <= 4) ? ((4 - $value) / 1) : 0));
        $veryOld = ($value <= 3) ? 0 : (($value <= 5) ? (($value - 3) / 2) : 1);
        
        return ['new' => $new, 'old' => $old, 'veryOld' => $veryOld];
    }

    // Membership functions for other components
    private function motherboardCompatibilityMembership($value) {
        $low = ($value <= 1) ? 1 : (($value <= 5) ? ((5 - $value) / 4) : 0);
        $medium = ($value <= 3) ? 0 : (($value <= 5) ? ((($value - 3) / 2)) : 
                 (($value <= 7) ? ((7 - $value) / 2) : 0));
        $high = ($value <= 5) ? 0 : (($value <= 10) ? (($value - 5) / 5) : 1);
        
        return ['low' => $low, 'medium' => $medium, 'high' => $high];
    }
	private function motherboardAgeMembership($value) {
        $new = ($value <= 0) ? 1 : (($value <= 1) ? (1 - $value) : 0);
        $old = ($value <= 1) ? 0 : (($value <= 3) ? (($value - 1) / 2) : 
               (($value <= 4) ? ((4 - $value) / 1) : 0));
        $veryOld = ($value <= 3) ? 0 : (($value <= 5) ? (($value - 3) / 2) : 1);
        
        return ['new' => $new, 'old' => $old, 'veryOld' => $veryOld];
    }
	
    private function vgaVramMembership($value) {
        $low = ($value <= 1) ? 1 : (($value <= 5) ? ((5 - $value) / 4) : 0);
        $medium = ($value <= 3) ? 0 : (($value <= 5) ? ((($value - 3) / 2)) : 
                 (($value <= 7) ? ((7 - $value) / 2) : 0));
        $high = ($value <= 5) ? 0 : (($value <= 10) ? (($value - 5) / 5) : 1);
        
        return ['low' => $low, 'medium' => $medium, 'high' => $high];
    }
	
	private function vgaAgeMembership($value) {
        $new = ($value <= 0) ? 1 : (($value <= 1) ? (1 - $value) : 0);
        $old = ($value <= 1) ? 0 : (($value <= 3) ? (($value - 1) / 2) : 
               (($value <= 4) ? ((4 - $value) / 1) : 0));
        $veryOld = ($value <= 3) ? 0 : (($value <= 5) ? (($value - 3) / 2) : 1);
        
        return ['new' => $new, 'old' => $old, 'veryOld' => $veryOld];
    }
	
    private function ramCapacityMembership($value) {
        $low = ($value <= 1) ? 1 : (($value <= 5) ? ((5 - $value) / 4) : 0);
        $medium = ($value <= 3) ? 0 : (($value <= 5) ? ((($value - 3) / 2)) : 
                 (($value <= 7) ? ((7 - $value) / 2) : 0));
        $high = ($value <= 5) ? 0 : (($value <= 10) ? (($value - 5) / 5) : 1);
        
        return ['low' => $low, 'medium' => $medium, 'high' => $high];
    }
	
	private function ramAgeMembership($value) {
        $new = ($value <= 0) ? 1 : (($value <= 1) ? (1 - $value) : 0);
        $old = ($value <= 1) ? 0 : (($value <= 3) ? (($value - 1) / 2) : 
               (($value <= 4) ? ((4 - $value) / 1) : 0));
        $veryOld = ($value <= 3) ? 0 : (($value <= 5) ? (($value - 3) / 2) : 1);
        
        return ['new' => $new, 'old' => $old, 'veryOld' => $veryOld];
    }
	private function storageCapacityMembership($value) {
        $low = ($value <= 1) ? 1 : (($value <= 5) ? ((5 - $value) / 4) : 0);
        $medium = ($value <= 3) ? 0 : (($value <= 5) ? ((($value - 3) / 2)) : 
                 (($value <= 7) ? ((7 - $value) / 2) : 0));
        $high = ($value <= 5) ? 0 : (($value <= 10) ? (($value - 5) / 5) : 1);
        
        return ['low' => $low, 'medium' => $medium, 'high' => $high];
    }

    private function storageAgeMembership($value) {
        $new = ($value <= 0) ? 1 : (($value <= 1) ? (1 - $value) : 0);
        $old = ($value <= 1) ? 0 : (($value <= 3) ? (($value - 1) / 2) : 
               (($value <= 4) ? ((4 - $value) / 1) : 0));
        $veryOld = ($value <= 3) ? 0 : (($value <= 5) ? (($value - 3) / 2) : 1);
        
        return ['new' => $new, 'old' => $old, 'veryOld' => $veryOld];
    }
	
	 // Casing membership functions
    private function casingPerformanceMembership($value) {
        $low = ($value <= 1) ? 1 : (($value <= 5) ? ((5 - $value) / 4) : 0);
        $medium = ($value <= 3) ? 0 : (($value <= 5) ? ((($value - 3) / 2)) : 
                 (($value <= 7) ? ((7 - $value) / 2) : 0));
        $high = ($value <= 5) ? 0 : (($value <= 10) ? (($value - 5) / 5) : 1);
        
        return ['low' => $low, 'medium' => $medium, 'high' => $high];
    }

    private function casingAgeMembership($value) {
        $new = ($value <= 0) ? 1 : (($value <= 1) ? (1 - $value) : 0);
        $old = ($value <= 1) ? 0 : (($value <= 3) ? (($value - 1) / 2) : 
               (($value <= 4) ? ((4 - $value) / 1) : 0));
        $veryOld = ($value <= 3) ? 0 : (($value <= 5) ? (($value - 3) / 2) : 1);
        
        return ['new' => $new, 'old' => $old, 'veryOld' => $veryOld];
    }

    private function calculateAssemblyCost($components) {
        $totalPerformance = 0;
        $totalAge = 0;
        $componentCount = 0;
        
        // Hitung rata-rata performance dan age dari semua komponen
        foreach ($components as $type => $id) {
            if (!empty($id)) {
                $componentData = $this->getComponentData($type, $id);
                if ($componentData) {
                    if (isset($componentData['performance'])) {
                        $totalPerformance += $componentData['performance'];
                    }
                    if (isset($componentData['age'])) {
                        $totalAge += $componentData['age'];
                    }
                    $componentCount++;
                }
            }
        }

        if ($componentCount == 0) return 0;

        $avgPerformance = $totalPerformance / $componentCount;
        $avgAge = $totalAge / $componentCount;

        // Hitung membership untuk rata-rata performance dan age
        $perfMembership = $this->processorPerformanceMembership($avgPerformance);
        $ageMembership = $this->processorAgeMembership($avgAge);

        // Tentukan biaya rakit berdasarkan kondisi
        if ($perfMembership['high'] > 0 && $ageMembership['new'] > 0) {
            return 1000000; // 1 juta untuk high performance dan new
        } elseif ($perfMembership['medium'] > 0 || 
                  ($perfMembership['high'] > 0 && $ageMembership['old'] > 0)) {
            return 600000; // 600rb untuk medium performance
        } else {
            return 300000; // 300rb untuk low performance
        }
    }

   public function calculateTotalPrice($userId, $components) {
    $totalPrice = 0;
    
    // Hitung total harga komponen
    foreach ($components as $type => $id) {
        if (!empty($id)) {
            $componentData = $this->getComponentData($type, $id);
            if ($componentData) {
                $totalPrice += floatval($componentData['price']);
            }
        }
    }

    // Tentukan biaya rakit berdasarkan harga total
    if ($totalPrice < 5000000) {
        $assemblyCost = 300000; // Biaya rakit 300rb
    } elseif ($totalPrice < 10000000) {
        $assemblyCost = 600000; // Biaya rakit 600rb
    } else {
        $assemblyCost = 1000000; // Biaya rakit 1jt
    }

    // Tambahkan biaya rakit ke total harga
    $totalPrice += $assemblyCost;

    // Simpan hasil perhitungan
    $this->saveCalculation($userId, $components, $totalPrice);

    return [
        'components_price' => $totalPrice - $assemblyCost,
        'assembly_cost' => $assemblyCost,
        'total_price' => $totalPrice
    ];
}


    private function getComponentData($type, $id) {
        $tables = [
            'processor' => 'processors',
            'motherboard' => 'motherboards',
            'vga' => 'vga_cards',
            'ram' => 'ram',
            'storage' => 'storage',
            'casing' => 'casing'
        ];

        $table = $tables[$type];
        $stmt = $this->db->prepare("SELECT * FROM $table WHERE id = ?");
        $stmt->execute([$id]);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    private function saveCalculation($userId, $components, $totalPrice) {
        $stmt = $this->db->prepare(
            "INSERT INTO calculations (
                user_id, processor_id, motherboard_id, vga_id, 
                ram_id, storage_id, casing_id, calculated_price
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
        );
        
        $stmt->execute([
            $userId,
            $components['processor'] ?? null,
            $components['motherboard'] ?? null,
            $components['vga'] ?? null,
            $components['ram'] ?? null,
            $components['storage'] ?? null,
            $components['casing'] ?? null,
            $totalPrice
        ]);

        return $this->db->lastInsertId();
    }
}
?>