//
//  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/ShardokEngine.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/settings/GameSettings.hpp"
#include "src/main/protobuf/net/eagle0/shardok/api/command_descriptor.pb.h"

namespace shardok {

using CommandProto = net::eagle0::shardok::api::CommandDescriptor;

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 GameSettingsSPtr& settings,
            const GameStateW& guessedState,
            const vector<CommandProto>& availableCommands,
            const vector<CommandProto>::const_iterator& fleeCommand,
            bool enableDebugLogging = false) -> FleeDecision;

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

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

}  // namespace shardok

#endif /* AIFleeDecisionCalculator_hpp */