// ==========================================================
// waystone_rules.cpp - POE2 Waystone & Map Modifier Rule Engine
// Poe2VisualTool v2.0.0 (100% C++23 Native Fusion)
// ==========================================================

#include "inspector/waystone_rules.hpp"
#include <algorithm>
#include <string>

namespace inspector {

namespace {

inline bool ContainsIgnoreCase(std::string_view haystack, std::string_view needle) noexcept {
    if (needle.empty()) return true;
    if (haystack.size() < needle.size()) return false;
    auto it = std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(),
        [](char h, char n) noexcept {
            return std::tolower(static_cast<unsigned char>(h)) == std::tolower(static_cast<unsigned char>(n));
        });
    return it != haystack.end();
}

} // namespace

const char* WaystoneRules::GetSeverityColorHex(ModSeverity severity) {
    switch (severity) {
        case ModSeverity::Brick:     return "#EF4444"; // Vivid Crimson Red
        case ModSeverity::Dangerous: return "#F97316"; // Bright Orange
        case ModSeverity::Reward:    return "#10B981"; // Emerald Green
        case ModSeverity::GodRoll:   return "#F59E0B"; // Radiant Amber/Gold
        case ModSeverity::Crafting:  return "#A855F7"; // Purple
        case ModSeverity::Neutral:
        default:                     return "#E2E8F0"; // Slate White
    }
}

bool WaystoneRules::IsBrickMod(std::string_view modText) {
    return EvaluateMod(modText).severity == ModSeverity::Brick;
}

bool WaystoneRules::IsRewardMod(std::string_view modText) {
    return EvaluateMod(modText).severity == ModSeverity::Reward;
}

ModEvaluation WaystoneRules::EvaluateMod(std::string_view modText) {
    ModEvaluation result;

    // ----------------------------------------------------------
    // 1. Cực kỳ nguy hiểm (BRICK MODS) - Tử trận chắc chắn / Hỏng Build
    // ----------------------------------------------------------
    // Reflect (Physical / Elemental / Chaos)
    if (ContainsIgnoreCase(modText, "reflect") &&
        (ContainsIgnoreCase(modText, "damage") || ContainsIgnoreCase(modText, "monsters reflect"))) {
        result.severity = ModSeverity::Brick;
        result.category = "Reflect";
        result.description = "Phản đòn sát thương vật lý / nguyên tố (Chết tức thì)";
        result.colorHex = GetSeverityColorHex(ModSeverity::Brick);
        result.scorePenalty = 50;
        return result;
    }

    // No Leech
    if (ContainsIgnoreCase(modText, "cannot leech")) {
        result.severity = ModSeverity::Brick;
        result.category = "No Leech";
        result.description = "Không thể hút Máu / Energy Shield / Mana";
        result.colorHex = GetSeverityColorHex(ModSeverity::Brick);
        result.scorePenalty = 45;
        return result;
    }

    // No Regen
    if (ContainsIgnoreCase(modText, "cannot regenerate") ||
        ContainsIgnoreCase(modText, "no life or mana regeneration")) {
        result.severity = ModSeverity::Brick;
        result.category = "No Regen";
        result.description = "Không thể hồi phục Máu, Mana hoặc Energy Shield";
        result.colorHex = GetSeverityColorHex(ModSeverity::Brick);
        result.scorePenalty = 50;
        return result;
    }

    // ----------------------------------------------------------
    // 2. Nguy hiểm cao (DANGEROUS MODS) - Cần chú ý cẩn thận
    // ----------------------------------------------------------
    // -Max Res
    if (ContainsIgnoreCase(modText, "maximum player resistances") ||
        (ContainsIgnoreCase(modText, "maximum") && ContainsIgnoreCase(modText, "resistances") && ContainsIgnoreCase(modText, "-"))) {
        result.severity = ModSeverity::Dangerous;
        result.category = "-Max Res";
        result.description = "Giảm kháng tối đa của nhân vật (Tăng mạnh sát thương gánh chịu)";
        result.colorHex = GetSeverityColorHex(ModSeverity::Dangerous);
        result.scorePenalty = 25;
        return result;
    }

    // Reduced Recovery Rate
    if (ContainsIgnoreCase(modText, "reduced recovery rate") ||
        ContainsIgnoreCase(modText, "less recovery rate")) {
        result.severity = ModSeverity::Dangerous;
        result.category = "Reduced Recovery";
        result.description = "Giảm tốc độ hồi phục Máu / Energy Shield";
        result.colorHex = GetSeverityColorHex(ModSeverity::Dangerous);
        result.scorePenalty = 20;
        return result;
    }

    // Anti-Crit
    if (ContainsIgnoreCase(modText, "reduced extra damage from critical hits")) {
        result.severity = ModSeverity::Dangerous;
        result.category = "Reduced Crit Dmg";
        result.description = "Quái giảm sát thương chí mạng phải nhận (Khắc tinh build Crit)";
        result.colorHex = GetSeverityColorHex(ModSeverity::Dangerous);
        result.scorePenalty = 15;
        return result;
    }

    // Monster Speed / Onslaught
    if (ContainsIgnoreCase(modText, "increased attack, cast and movement speed") ||
        ContainsIgnoreCase(modText, "onslaught")) {
        result.severity = ModSeverity::Dangerous;
        result.category = "Monster Speed";
        result.description = "Tăng mạnh tốc độ hành động và xuất chiêu của quái";
        result.colorHex = GetSeverityColorHex(ModSeverity::Dangerous);
        result.scorePenalty = 15;
        return result;
    }

    // Extra Damage as Elemental / Chaos
    if (ContainsIgnoreCase(modText, "as extra") && ContainsIgnoreCase(modText, "damage")) {
        result.severity = ModSeverity::Dangerous;
        result.category = "Extra Damage";
        result.description = "Quái gây thêm sát thương nguyên tố / hỗn loạn";
        result.colorHex = GetSeverityColorHex(ModSeverity::Dangerous);
        result.scorePenalty = 20;
        return result;
    }

    // ----------------------------------------------------------
    // 3. Thưởng cao / An toàn (REWARD MODS)
    // ----------------------------------------------------------
    if (ContainsIgnoreCase(modText, "increased quantity of items found")) {
        result.severity = ModSeverity::Reward;
        result.category = "Quantity";
        result.description = "Tăng số lượng vật phẩm rơi";
        result.colorHex = GetSeverityColorHex(ModSeverity::Reward);
        return result;
    }

    if (ContainsIgnoreCase(modText, "increased rarity of items found")) {
        result.severity = ModSeverity::Reward;
        result.category = "Rarity";
        result.description = "Tăng độ hiếm vật phẩm rơi";
        result.colorHex = GetSeverityColorHex(ModSeverity::Reward);
        return result;
    }

    if (ContainsIgnoreCase(modText, "increased monster pack size")) {
        result.severity = ModSeverity::Reward;
        result.category = "Pack Size";
        result.description = "Tăng mật độ bầy quái vật";
        result.colorHex = GetSeverityColorHex(ModSeverity::Reward);
        return result;
    }

    // ----------------------------------------------------------
    // 4. Thuộc tính bình thường (NEUTRAL)
    // ----------------------------------------------------------
    result.severity = ModSeverity::Neutral;
    result.colorHex = GetSeverityColorHex(ModSeverity::Neutral);
    return result;
}

} // namespace inspector
