//
// Core types for abstract MCTS implementation
//

#ifndef EAGLE0_MCTS_TYPES_HPP
#define EAGLE0_MCTS_TYPES_HPP

#include <stdexcept>
#include <string>

namespace shardok {
namespace mcts {

// Exception thrown when MCTS encounters an internal error that indicates a bug
class MCTSInternalError : public std::logic_error {
public:
    explicit MCTSInternalError(const std::string& message) : std::logic_error(message) {}
};

// Abstract player identifier type
using MCTSPlayerId = int;

// Simulation policy for MCTS rollouts
enum class MCTSSimulationPolicy {
    RANDOM,                   // Pure random selection
    FILTERED_RANDOM,          // Random from filtered actions
    BEST_IMMEDIATE,           // Choose best immediate score
    WEIGHTED_BEST_IMMEDIATE,  // Random weighted by score ranking
    WEIGHTED_HEURISTIC        // Random weighted by fast heuristics (no score evaluation)
};

// Backpropagation policy for MCTS tree updates
enum class MCTSBackpropagationPolicy {
    AVERAGING,  // Traditional MCTS averaging (for stochastic/single-player games)
    MINIMAX     // Minimax backup (for deterministic adversarial games)
};

// Configuration for MCTS algorithm
struct MCTSConfig {
    double explorationConstant = 1.414;  // UCB1 constant (sqrt(2) by default)
    int maxSimulationDepth = 1000;       // Maximum depth for rollout
    int maxTreeDepth = 2000;             // Maximum tree depth to prevent stack overflow
    bool useMultithreading = true;       // Enable parallel MCTS
    int numThreads = 16;                 // Number of threads for parallel MCTS
    MCTSSimulationPolicy simulationPolicy = MCTSSimulationPolicy::BEST_IMMEDIATE;
    MCTSBackpropagationPolicy backpropagationPolicy = MCTSBackpropagationPolicy::AVERAGING;
    int maxPlayerFlips = 0;          // Maximum number of player changes for tree expansion
                                     // (0 = expand through current player's turn only,
                                     //  1 = expand through opponent's first response, etc.)
    int maxSimulationFlips = 0;      // Maximum player flips for leaf evaluation
                                     // When evaluating a leaf at playerFlips < maxSimulationFlips,
                                     // simulate forward to this phase for fair comparison
                                     // (default 0 = evaluate leaves as-is, backward compatible)
    uint32_t randomSeed = 0;         // 0 = seed nondeterministically
    std::string debugDumpPath = "";  // If non-empty, dump MCTS tree to this file path
};

}  // namespace mcts
}  // namespace shardok

#endif  // EAGLE0_MCTS_TYPES_HPP
