//
//  AIFleeDecisionCalculator.hpp
//  eagle0
//
//  Handles AI flee decision logic including combat success estimation
//  and flee vs fight evaluation for final round scenarios
//

#ifndef AIFleeDecisionCalculator_hpp
#define AIFleeDecisionCalculator_hpp

#include "src/main/cpp/net/eagle0/shardok/library/ShardokCommand.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/ShardokEngine.hpp"

namespace shardok {

class AIFleeDecisionCalculator {
public:
    // Configuration for flee decision thresholds
    struct FleeThresholds {
        int minimumFleeOddsThreshold;  // Minimum flee success odds to consider fleeing
        int desperateFleeThreshold;    // Flee threshold when combat is hopeless
    };

    // Result of flee vs fight evaluation
    struct FleeDecision {
        bool shouldFlee;
        size_t commandIndex;    // Index of command to execute (flee or fight)
        const char* reasoning;  // Debug explanation of decision
    };

    // Evaluate whether to flee or fight in the final round
    [[nodiscard]] static auto EvaluateFleeVsFight(
            PlayerId playerId,
            const GameStateW& guessedState,
            const CommandListSPtr& availableCommands,
            const CommandList::const_iterator& fleeCommand,
            int maxRounds,
            int minimumFleeOddsThreshold,
            int desperateFleeThreshold,
            bool enableDebugLogging = false) -> FleeDecision;

    // Estimate probability of combat success for the attacker
    [[nodiscard]] static auto EstimateCombatSuccess(
            PlayerId attackerPlayerId,
            const GameStateW& guessedState,
            int maxRounds) -> double;

    // Determine if the attacker should consider fleeing based on combat odds
    // Returns true if fleeing should be considered as an option
    [[nodiscard]] static auto ShouldConsiderFleeing(
            PlayerId attackerPlayerId,
            const GameStateW& guessedState,
            int maxRounds,
            double fleeConsiderationThreshold = 0.5) -> bool;

private:
    // Helper to get flee command index
    [[nodiscard]] static auto GetFleeCommandIndex(
            const CommandList::const_iterator& fleeCommand,
            const CommandListSPtr& availableCommands) -> size_t;
};

}  // namespace shardok

#endif /* AIFleeDecisionCalculator_hpp */