//
// Created by Dan Crosby on 07/04/25.
//

#ifndef EAGLE0_SHARDOK_AI_AI_TIME_BUDGET_HPP
#define EAGLE0_SHARDOK_AI_AI_TIME_BUDGET_HPP

#include <atomic>
#include <chrono>
#include <memory>

#include "src/main/cpp/net/eagle0/shardok/library/GameStateW.hpp"
#include "src/main/cpp/net/eagle0/shardok/library/ShardokCTypes.h"

namespace shardok {

// Forward declarations
class GameSettings;
using GameSettingsSPtr = std::shared_ptr<GameSettings>;

// RAII counter for tracking concurrent AI command evaluations
class AIEvaluationCounter {
    static std::atomic<int> activeCount;

public:
    AIEvaluationCounter();
    ~AIEvaluationCounter();
    AIEvaluationCounter(const AIEvaluationCounter&) = delete;
    auto operator=(const AIEvaluationCounter&) -> AIEvaluationCounter& = delete;
    AIEvaluationCounter(AIEvaluationCounter&&) = delete;
    auto operator=(AIEvaluationCounter&&) -> AIEvaluationCounter& = delete;

    [[nodiscard]] static int GetCurrentCount();
};

// Configuration structure for iterative deepening time budget
struct AITimeBudget {
    std::chrono::milliseconds remainingBudget;  // Time budget remaining (decremented as used)
    size_t minDepthRequired;                    // Minimum depth from minLookaheadTurns
    bool isCloseToEnemy;                        // Proximity flag for budget selection
};

// Calculate time budget based on proximity to enemies and castles
// Time budget is calculated dynamically based on number of available commands:
//   budget = msPerCommand × numCommands (clamped to 200-5000ms)
// If isAllAiBattle is true, uses allAiBattleTimeBudgetMaximum instead of the normal maximum.
[[nodiscard]] auto CalculateTimeBudget(
        PlayerId playerId,
        const GameSettingsSPtr& settings,
        const GameStateW& state,
        size_t numCommands,
        bool isAllAiBattle) -> AITimeBudget;

}  // namespace shardok

#endif  // EAGLE0_SHARDOK_AI_AI_TIME_BUDGET_HPP
