<?php
/* 
 * 等级计算核心逻辑 (带颜色与图标系统)
 */

/**
 * 获取升级到下一级所需的累积币数
 */
function getRequiredCoinsForNextLevel($current_level) {
    $tier = floor($current_level / 10);
    return ($tier + 1) * 10;
}

/**
 * 检查并更新用户等级
 */
function checkAndUpdateLevel($user_id, $conn) {
    // 1. 计算总累积量
    $stmt = mysqli_prepare($conn, "SELECT SUM(ABS(change_amount)) as total_acc FROM coin_logs WHERE user_id = ?");
    mysqli_stmt_bind_param($stmt, "i", $user_id);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    $row = mysqli_fetch_assoc($result);
    $total_accumulated = $row['total_acc'] ?? 0;

    // 2. 获取现有等级
    $stmt_cur = mysqli_prepare($conn, "SELECT level FROM users WHERE id = ?");
    mysqli_stmt_bind_param($stmt_cur, "i", $user_id);
    mysqli_stmt_execute($stmt_cur);
    $res_cur = mysqli_stmt_get_result($stmt_cur);
    $user = mysqli_fetch_assoc($res_cur);
    $current_level = intval($user['level']);

    // 3. 计算理论等级
    $new_level = 0;
    $used_coins = 0;
    
    while (true) {
        $need = getRequiredCoinsForNextLevel($new_level);
        if (($total_accumulated - $used_coins) >= $need) {
            $used_coins += $need;
            $new_level++;
        } else {
            break;
        }
        if ($new_level > 100) break;
    }

    if ($new_level > $current_level) {
        $update = mysqli_prepare($conn, "UPDATE users SET level = ? WHERE id = ?");
        mysqli_stmt_bind_param($update, "ii", $new_level, $user_id);
        mysqli_stmt_execute($update);
        return $new_level;
    }
    
    return false;
}

/**
 * 获取等级样式配置 (颜色、图标)
 * 规则：每10级一个色系，1-5级浅色，6-10级深色
 */
function getLevelStyle($level) {
    // 默认配置
    $style = [
        'icon' => '🌟', // 默认图标
        'color' => '#6c757d', // 默认颜色
        'name' => '新手'
    ];

    // 等级区间判断 (每10级一个阶段)
    $tier = floor($level / 10); // 0代表1-10级, 1代表11-20级...
    $step = $level % 10; // 在当前阶段的位置 (0-9)

    if ($level == 0) {
        return ['icon' => '🌱', 'color' => '#adb5bd', 'name' => '待激活'];
    }

    // 定义色系库 (每个阶段一个主色调)
    // 格式：['浅色', '深色', '图标', '名称']
    $themes = [
        0 => ['#495057', '#000000', '🌟', '灰阶'],      // 1-10级：黑色系
        1 => ['#74c0fc', '#1864ab', '💎', '蔚蓝'],      // 11-20级：蓝色系
        2 => ['#66d9e8', '#0b7285', '🕊️', '青空'],      // 21-30级：青色系
        3 => ['#8ce99a', '#2b8a3e', '🌿', '翠绿'],      // 31-40级：绿色系
        4 => ['#ffd43b', '#e67700', '🦊', '金辉'],      // 41-50级：黄色/橙色系
        5 => ['#ffa8a8', '#c92a2a', '🔥', '赤焰'],      // 51-60级：红色系
        6 => ['#eebefa', '#9c36b5', '🔮', '幻紫'],      // 61-70级：紫色系
        7 => ['#fcc2d7', '#c2255c', '🌹', '浪漫'],      // 71-80级：粉色系
        8 => ['#b2f2bb', '#087f5b', '⚔️', '暗金'],      // 81-90级：深绿/暗金
        9 => ['#e9ecef', '#212529', '👑', '至尊'],      // 91-100级：黑白至尊
    ];

    // 获取当前阶段配置
    $current_theme = $themes[$tier] ?? $themes[0];
    $light_color = $current_theme[0];
    $dark_color = $current_theme[1];
    $icon = $current_theme[2];
    $name = $current_theme[3];

    // 计算颜色深浅 (1-5级浅色，6-10级深色)
    // 使用线性插值：$step 越大，颜色越深
    // 0-4 step: 偏向 light_color
    // 5-9 step: 偏向 dark_color
    
    // 简单算法：前5级用浅色，后5级用深色
    if ($step < 5) {
        $final_color = $light_color;
    } else {
        // 6-10级逐渐变深
        $final_color = $dark_color;
    }

    // 满级 (100级) 特殊处理
    if ($level >= 100) {
        return ['icon' => '🏆', 'color' => 'linear-gradient(45deg, #FFD700, #FF4500)', 'name' => '传说'];
    }

    return [
        'icon' => $icon,
        'color' => $final_color,
        'name' => $name
    ];
}
?>
