<?php
class FuzzyTsukamoto {
    private $weights = [
    'processor' => 0.20,    // turun dari 0.25
    'motherboard' => 0.15,  // turun dari 0.20
    'vga' => 0.20,         // turun dari 0.25
    'ram' => 0.15,         // tetap
    'storage' => 0.10,     // tetap
    'casing' => 0.05,      // tetap
    'power_supply' => 0.15 // komponen baru
];

    // PROCESSOR FUNCTIONS
    private function processorPerformanceMembership($value) {
        $result = [
            'low' => 0,
            'medium' => 0,
            'high' => 0
        ];
        
        // Low (1-4)
        if ($value <= 1) $result['low'] = 1;
        else if ($value > 1 && $value < 4) $result['low'] = (4 - $value) / 3;
        
        // Medium (3-7)
        if ($value >= 4 && $value <= 6) $result['medium'] = 1;
        else if ($value > 3 && $value < 4) $result['medium'] = ($value - 3) / 1;
        else if ($value > 6 && $value < 7) $result['medium'] = (7 - $value) / 1;
        
        // High (6-10)
        if ($value >= 9) $result['high'] = 1;
        else if ($value > 6 && $value < 9) $result['high'] = ($value - 6) / 3;
        
        return $result;
    }

    // AGE MEMBERSHIP
    private function ageMembership($value) {
        $result = [
            'baru' => 0,
            'lama' => 0,
            'sangat_lama' => 0
        ];
        
        // Baru (0-1.5)
        if ($value <= 0.5) $result['baru'] = 1;
        else if ($value > 0.5 && $value < 1.5) $result['baru'] = (1.5 - $value) / 1;
        
        // Lama (1-3)
        if ($value >= 1.5 && $value <= 2.5) $result['lama'] = 1;
        else if ($value > 1 && $value < 1.5) $result['lama'] = ($value - 1) / 0.5;
        else if ($value > 2.5 && $value < 3) $result['lama'] = (3 - $value) / 0.5;
        
        // Sangat Lama (2.5-5)
        if ($value >= 4) $result['sangat_lama'] = 1;
        else if ($value > 2.5 && $value < 4) $result['sangat_lama'] = ($value - 2.5) / 1.5;
        
        return $result;
    }

    // RAM TYPE MEMBERSHIP
    private function ramTypeMembership($value) {
        $result = [
            'ddr3' => 0,
            'ddr4' => 0,
            'ddr5' => 0
        ];
        
        // DDR3 (0-0.4)
        if ($value <= 0.2) $result['ddr3'] = 1;
        else if ($value > 0.2 && $value < 0.4) $result['ddr3'] = (0.4 - $value) / 0.2;
        
        // DDR4 (0.3-0.7)
        if ($value >= 0.4 && $value <= 0.6) $result['ddr4'] = 1;
        else if ($value > 0.3 && $value < 0.4) $result['ddr4'] = ($value - 0.3) / 0.1;
        else if ($value > 0.6 && $value < 0.7) $result['ddr4'] = (0.7 - $value) / 0.1;
        
        // DDR5 (0.6-1)
        if ($value >= 0.8) $result['ddr5'] = 1;
        else if ($value > 0.6 && $value < 0.8) $result['ddr5'] = ($value - 0.6) / 0.2;
        
        return $result;
    }

    // STORAGE TYPE MEMBERSHIP
    private function storageTypeMembership($value) {
        $result = [
            'hdd' => 0,
            'ssd_sata' => 0,
            'ssd_nvme' => 0
        ];
        
        // HDD (0-0.4)
        if ($value <= 0.2) $result['hdd'] = 1;
        else if ($value > 0.2 && $value < 0.4) $result['hdd'] = (0.4 - $value) / 0.2;
        
        // SSD SATA (0.3-0.7)
        if ($value >= 0.4 && $value <= 0.6) $result['ssd_sata'] = 1;
        else if ($value > 0.3 && $value < 0.4) $result['ssd_sata'] = ($value - 0.3) / 0.1;
        else if ($value > 0.6 && $value < 0.7) $result['ssd_sata'] = (0.7 - $value) / 0.1;
        
        // SSD NVMe (0.6-1)
        if ($value >= 0.8) $result['ssd_nvme'] = 1;
        else if ($value > 0.6 && $value < 0.8) $result['ssd_nvme'] = ($value - 0.6) / 0.2;
        
        return $result;
    }

    // CAPACITY MEMBERSHIP (Used for RAM and Storage)
    private function capacityMembership($value) {
        return $this->processorPerformanceMembership($value); // Same logic as performance
    }

    // PHYSICAL CONDITION MEMBERSHIP
    private function physicalConditionMembership($value) {
        $result = [
            'rusak' => 0,
            'normal' => 0,
            'baik' => 0
        ];
        
        // Rusak (0-2)
        if ($value <= 1) $result['rusak'] = 1;
        else if ($value > 1 && $value < 2) $result['rusak'] = (2 - $value) / 1;
        
        // Normal (1.5-3.5)
        if ($value >= 2 && $value <= 3) $result['normal'] = 1;
        else if ($value > 1.5 && $value < 2) $result['normal'] = ($value - 1.5) / 0.5;
        else if ($value > 3 && $value < 3.5) $result['normal'] = (3.5 - $value) / 0.5;
        
        // Baik (3-5)
        if ($value >= 4) $result['baik'] = 1;
        else if ($value > 3 && $value < 4) $result['baik'] = ($value - 3) / 1;
        
        return $result;
    }

    // MARKUP RULES
    private function getMarkupRules($componentType) {
        $rules = [
            'processor' => [
                'high_baru' => ['markup' => 1.30],
                'high_lama' => ['markup' => 1.25],
                'high_sangat_lama' => ['markup' => 1.20],
                'medium_baru' => ['markup' => 1.25],
                'medium_lama' => ['markup' => 1.20],
                'medium_sangat_lama' => ['markup' => 1.15],
                'low_baru' => ['markup' => 1.20],
                'low_lama' => ['markup' => 1.15],
                'low_sangat_lama' => ['markup' => 1.10],
                'default' => ['markup' => 1.15]
            ],
            'motherboard' => [
                'high_baru' => ['markup' => 1.30],
                'high_lama' => ['markup' => 1.25],
                'high_sangat_lama' => ['markup' => 1.20],
                'medium_baru' => ['markup' => 1.25],
                'medium_lama' => ['markup' => 1.20],
                'medium_sangat_lama' => ['markup' => 1.15],
                'low_baru' => ['markup' => 1.20],
                'low_lama' => ['markup' => 1.15],
                'low_sangat_lama' => ['markup' => 1.10],
                'default' => ['markup' => 1.15]
            ],
            'vga' => [
                'high_baru' => ['markup' => 1.30],
                'high_lama' => ['markup' => 1.25],
                'high_sangat_lama' => ['markup' => 1.20],
                'medium_baru' => ['markup' => 1.25],
                'medium_lama' => ['markup' => 1.20],
                'medium_sangat_lama' => ['markup' => 1.15],
                'low_baru' => ['markup' => 1.20],
                'low_lama' => ['markup' => 1.15],
                'low_sangat_lama' => ['markup' => 1.10],
                'default' => ['markup' => 1.15]
            ],
            'ram' => [
                'high_ddr5' => ['markup' => 1.30],
                'high_ddr4' => ['markup' => 1.25],
                'high_ddr3' => ['markup' => 1.20],
                'medium_ddr5' => ['markup' => 1.25],
                'medium_ddr4' => ['markup' => 1.20],
                'medium_ddr3' => ['markup' => 1.15],
                'low_ddr5' => ['markup' => 1.20],
                'low_ddr4' => ['markup' => 1.15],
                'low_ddr3' => ['markup' => 1.10],
                'default' => ['markup' => 1.15]
            ],
            'storage' => [
                'high_ssd_nvme' => ['markup' => 1.30],
                'high_ssd_sata' => ['markup' => 1.25],
                'high_hdd' => ['markup' => 1.20],
                'medium_ssd_nvme' => ['markup' => 1.25],
                'medium_ssd_sata' => ['markup' => 1.20],
                'medium_hdd' => ['markup' => 1.15],
                'low_ssd_nvme' => ['markup' => 1.20],
                'low_ssd_sata' => ['markup' => 1.15],
                'low_hdd' => ['markup' => 1.10],
                'default' => ['markup' => 1.15]
            ],
                            'power_supply' => [
                    'high_baru' => ['markup' => 1.30],
                    'high_lama' => ['markup' => 1.25],
                    'high_sangat_lama' => ['markup' => 1.20],
                    'medium_baru' => ['markup' => 1.25],
                    'medium_lama' => ['markup' => 1.20],
                    'medium_sangat_lama' => ['markup' => 1.15],
                    'low_baru' => ['markup' => 1.20],
                    'low_lama' => ['markup' => 1.15],
                    'low_sangat_lama' => ['markup' => 1.10],
                    'default' => ['markup' => 1.15]
                ],
            'casing' => [
                'high_baik' => ['markup' => 1.30],
                'high_normal' => ['markup' => 1.25],
                'high_rusak' => ['markup' => 1.20],
                'medium_baik' => ['markup' => 1.25],
                'medium_normal' => ['markup' => 1.20],
                'medium_rusak' => ['markup' => 1.15],
                'low_baik' => ['markup' => 1.20],
                'low_normal' => ['markup' => 1.15],
                'low_rusak' => ['markup' => 1.10],
                'default' => ['markup' => 1.15]
            ]
        ];
        
        return $rules[$componentType] ?? ['default' => ['markup' => 1.15]];
    }

    // MAIN CALCULATION
    public function calculateSellingPrice($components) {
        $calculations = [];
        $totalPrice = 0;
        
        foreach ($components as $type => $component) {
            if (!$component) continue;
            
            $calc = [
                'type' => $type,
                'name' => $component['name'],
                'base_price' => $component['price'],
                'rules' => []
            ];
            
            $weightedPrices = 0;
            $totalAlpha = 0;
            
            switch ($type) {
                case 'processor':
                    $performance = $this->processorPerformanceMembership($component['performance']);
                    $age = $this->ageMembership($component['age']);
                    $rules = $this->getMarkupRules('processor');
                    
                    foreach ($performance as $p_key => $p_val) {
                        foreach ($age as $a_key => $a_val) {
                            $alpha = min($p_val, $a_val);
                            if ($alpha > 0) {
                                $rule_key = "{$p_key}_{$a_key}";
                                $markup = $rules[$rule_key]['markup'] ?? $rules['default']['markup'];
                                
                                $price_contribution = $component['price'] * $markup;
                                $weightedPrices += ($price_contribution * $alpha);
                                $totalAlpha += $alpha;
                                
                                $calc['rules'][] = [
                                    'condition' => "IF Performance is {$p_key} AND Age is {$a_key}",
                                    'alpha' => $alpha,
                                    'markup' => $markup,
                                    'weighted_price' => $price_contribution * $alpha
                                ];
                            }
                        }
                    }
                    break;

                case 'motherboard':
                    $performance = $this->processorPerformanceMembership($component['compatibility']);
                    $age = $this->ageMembership($component['age']);
                    $rules = $this->getMarkupRules('motherboard');
                    
                    foreach ($performance as $p_key => $p_val) {
                        foreach ($age as $a_key => $a_val) {
                            $alpha = min($p_val, $a_val);
                            if ($alpha > 0) {
                                $rule_key = "{$p_key}_{$a_key}";
                                $markup = $rules[$rule_key]['markup'] ?? $rules['default']['markup'];
                                
                                $price_contribution = $component['price'] * $markup;
                                $weightedPrices += ($price_contribution * $alpha);
                                $totalAlpha += $alpha;
                                
                                $calc['rules'][] = [
                                    'condition' => "IF Compatibility is {$p_key} AND Age is {$a_key}",
                                    'alpha' => $alpha,
                                    'markup' => $markup,
                                    'weighted_price' => $price_contribution * $alpha
                                ];
                            }
                        }
                    }
                    break;

                case 'vga':
                    $performance = $this->processorPerformanceMembership($component['vram_capacity']);
                    $age = $this->ageMembership($component['age']);
                    $rules = $this->getMarkupRules('vga');
                    
                    foreach ($performance as $p_key => $p_val) {
                        foreach ($age as $a_key => $a_val) {
                            $alpha = min($p_val, $a_val);
                            if ($alpha > 0) {
                                $rule_key = "{$p_key}_{$a_key}";
                                $markup = $rules[$rule_key]['markup'] ?? $rules['default']['markup'];
                                
                                $price_contribution = $component['price'] * $markup;
                                $weightedPrices += ($price_contribution * $alpha);
                                $totalAlpha += $alpha;
                                
                                $calc['rules'][] = [
                                    'condition' => "IF VRAM is {$p_key} AND Age is {$a_key}",
                                    'alpha' => $alpha,
                                    'markup' => $markup,
                                    'weighted_price' => $price_contribution * $alpha
                                ];
                            }
                        }
                    }
                    break;

                case 'ram':
                    $capacity = $this->capacityMembership($component['capacity']);
                    $ramType = $this->ramTypeMembership($component['type']);
                    $rules = $this->getMarkupRules('ram');
                    
                    foreach ($capacity as $c_key => $c_val) {
                        foreach ($ramType as $t_key => $t_val) {
                            $alpha = min($c_val, $t_val);
                            if ($alpha > 0) {
                                $rule_key = "{$c_key}_{$t_key}";
                                $markup = $rules[$rule_key]['markup'] ?? $rules['default']['markup'];
                                
                                $price_contribution = $component['price'] * $markup;
                                $weightedPrices += ($price_contribution * $alpha);
                                $totalAlpha += $alpha;
                                
                                $calc['rules'][] = [
                                    'condition' => "IF Capacity is {$c_key} AND Type is {$t_key}",
                                    'alpha' => $alpha,
                                    'markup' => $markup,
                                    'weighted_price' => $price_contribution * $alpha
                                ];
                            }
                        }
                    }
                    break;

                case 'storage':
                    $capacity = $this->capacityMembership($component['capacity']);
                    $storageType = $this->storageTypeMembership($component['type']);
                    $rules = $this->getMarkupRules('storage');
                    
                    foreach ($capacity as $c_key => $c_val) {
                        foreach ($storageType as $t_key => $t_val) {
                            $alpha = min($c_val, $t_val);
                            if ($alpha > 0) {
                                $rule_key = "{$c_key}_{$t_key}";
                                $markup = $rules[$rule_key]['markup'] ?? $rules['default']['markup'];
                                
                                $price_contribution = $component['price'] * $markup;
                                $weightedPrices += ($price_contribution * $alpha);
                                $totalAlpha += $alpha;
                                
                                $calc['rules'][] = [
                                    'condition' => "IF Capacity is {$c_key} AND Type is {$t_key}",
                                    'alpha' => $alpha,
                                    'markup' => $markup,
                                    'weighted_price' => $price_contribution * $alpha
                                ];
                            }
                        }
                    }
                    break;
        
                    case 'power_supply':
    $quality = $this->processorPerformanceMembership($component['quality']);
    $age = $this->ageMembership($component['age']);
    $rules = $this->getMarkupRules('power_supply');
    
    $componentWeight = 0;
    $componentAlpha = 0;
    $calc['rules'] = [];
    
    foreach ($quality as $q_key => $q_val) {
        foreach ($age as $a_key => $a_val) {
            $alpha = min($q_val, $a_val);
            if ($alpha > 0) {
                $rule_key = "{$q_key}_{$a_key}";
                $markup = $rules[$rule_key]['markup'] ?? $rules['default']['markup'];
                
                $price_contribution = $component['price'] * $markup;
                $weighted_price = $price_contribution * $alpha;
                
                $componentWeight += $weighted_price;
                $componentAlpha += $alpha;
                
                $calc['rules'][] = [
                    'condition' => "IF Quality is {$q_key} AND Age is {$a_key}",
                    'alpha' => $alpha,
                    'markup' => $markup,
                    'weighted_price' => $weighted_price
                ];
            }
        }
    }
    
    // Hitung harga final untuk power supply
    if ($componentAlpha > 0) {
        $calc['final_price'] = $componentWeight / $componentAlpha;
    } else {
        $calc['final_price'] = $component['price'] * 1.15; // default markup
    }
    
    $calc['base_price'] = $component['price'];
    $calculations[] = $calc;
    
    // TIDAK perlu menambahkan ke totalPrice karena akan dihitung di akhir
    break;
        $finalPrice = 0;
    foreach ($calculations as $calc) {
        $finalPrice += $calc['final_price'];
    }

    return [
        'price' => $finalPrice,
        'calculations' => $calculations,
        'component_weights' => $this->weights
    ];

                case 'casing':
                    $quality = $this->processorPerformanceMembership($component['build_quality']);
                    $condition = $this->physicalConditionMembership($component['physical_condition']);
                    $rules = $this->getMarkupRules('casing');
                    
                    foreach ($quality as $q_key => $q_val) {
                        foreach ($condition as $c_key => $c_val) {
                            $alpha = min($q_val, $c_val);
                            if ($alpha > 0) {
                                $rule_key = "{$q_key}_{$c_key}";
                                $markup = $rules[$rule_key]['markup'] ?? $rules['default']['markup'];
                                
                                $price_contribution = $component['price'] * $markup;
                                $weightedPrices += ($price_contribution * $alpha);
                                $totalAlpha += $alpha;
                                
                                $calc['rules'][] = [
                                    'condition' => "IF Build Quality is {$q_key} AND Condition is {$c_key}",
                                    'alpha' => $alpha,
                                    'markup' => $markup,
                                    'weighted_price' => $price_contribution * $alpha
                                ];
                            }
                        }
                    }
                    break;
            }
            
            // Hitung harga komponen
            if ($totalAlpha > 0) {
                $componentPrice = $weightedPrices / $totalAlpha;
                $calc['final_price'] = $componentPrice;
                $totalPrice += $componentPrice;
            } else {
                $calc['final_price'] = $component['price'] * 1.15; // Default markup
                $totalPrice += $calc['final_price'];
            }
            
            $calculations[] = $calc;
        }
        
        return [
            'price' => $totalPrice,
            'calculations' => $calculations,
            'component_weights' => $this->weights
        ];
    }
}
?>