<?php
/**
 * 時間大富翁 - 卡牌規則與結算引擎 (Card & Buff Rules Engine)
 */
class CardEngine {
    
    // ==========================================
    // 1. 日常任務結算邏輯
    // ==========================================
    public static function calculateTask($original_earned, $stars, $active_buffs) {
        $quality_factor = 1.0;
        $buff_desc = "";
        $used_buff_ids = [];
        $has_shield_triggered = false; // 標記護盾是否觸發

        if ($stars === 3) $quality_factor = 1.2;
        if ($stars === 1) $quality_factor = 0.5;

        // ★ 護盾魔法 (shield)：檢查身上是否有護盾
        $shield = self::findBuff($active_buffs, 'shield');
        if ($shield) {
            // 防禦 1：擋下家長的 1 星差評
            if ($stars === 1) {
                $quality_factor = 1.0; 
                $buff_desc .= " [🛡️ 護盾發動：成功擋下 1星 評價懲罰]";
                $has_shield_triggered = true;
            }
        }

        $base_reward = $original_earned * $quality_factor;
        $final_reward = $base_reward;

        // ★ 倍率魔法 (multiplier)：只能放大「正收入」，倒扣不能放大
        $multiplier_buff = self::findBuff($active_buffs, 'multiplier');
        if ($multiplier_buff && $base_reward > 0) { 
            $multiplier_val = (float)$multiplier_buff['effect_value'];
            $final_reward *= $multiplier_val;
            $buff_desc .= " [🧪 魔法發動：任務獎勵 x{$multiplier_val} 倍！]";
            $used_buff_ids[] = $multiplier_buff['id'];
        }

        // ★ 固定增減魔法 (fixed_time)：無腦加上去 (有可能是扣時間卡)
        $fixed_buff = self::findBuff($active_buffs, 'fixed_time');
        if ($fixed_buff) {
            $fixed_val = (int)$fixed_buff['effect_value'];
            $final_reward += $fixed_val;
            $sign = ($fixed_val > 0) ? '+' : '';
            $buff_desc .= " [⏳ 時光干涉：{$sign}{$fixed_val} 分鐘！]";
            $used_buff_ids[] = $fixed_buff['id'];
        }

        // =====================================
        // ★ 終極護盾防禦結算 (防禦 2：擋下遲到/超時的負分倒扣)
        // =====================================
        if ($final_reward < 0 && $shield) {
            $final_reward = 0; // 強制歸零，不倒扣！
            if (!$has_shield_triggered) {
                $buff_desc .= " [🛡️ 護盾發動：成功擋下超時倒扣懲罰，免扣分！]";
            }
            $has_shield_triggered = true;
        }
        
        // 如果護盾在整個過程中有發揮作用，就把它的 ID 記錄下來準備消耗掉
        if ($has_shield_triggered && $shield) {
             if (!in_array($shield['id'], $used_buff_ids)) {
                 $used_buff_ids[] = $shield['id'];
             }
        }

        return [
            'final_earned' => round($final_reward),
            'buff_desc' => $buff_desc,
            'used_buff_ids' => $used_buff_ids
        ];
    }

    public static function calculateStockBuffs($stock_id, $active_buffs) { return []; }
    public static function calculateEstateBuffs($estate_id, $active_buffs) { return []; }

    private static function findBuff($buffs, $type) {
        foreach ($buffs as $b) {
            if ($b['buff_type'] === $type && $b['active_count'] > 0) {
                return $b;
            }
        }
        return null;
    }
}
?>