//
// Shardok-specific action adapter implementation
//

#include "ShardokAction.hpp"

#include <sstream>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-redundant-constexpr-static-def"
#include "src/main/protobuf/net/eagle0/shardok/common/command_type.pb.h"
#pragma clang diagnostic pop

namespace shardok::mcts {

// Constructor: extract and store just the essential fields
ShardokAction::ShardokAction(
        size_t index,
        CommandType type,
        PlayerId player,
        int actorId,
        int targetRow,
        int targetCol,
        bool hasOdds)
    : commandIndex_(index),
      type_(type),
      player_(player),
      actorId_(actorId),
      targetRow_(targetRow),
      targetCol_(targetCol),
      hasOdds_(hasOdds) {}

std::string ShardokAction::getDescription() const {
    std::stringstream ss;

    // Show player
    ss << "P" << static_cast<int>(player_) << " ";

    ss << net::eagle0::shardok::common::CommandType_Name(type_);

    if (actorId_ >= 0) { ss << " Unit:" << actorId_; }

    if (targetRow_ >= 0 && targetCol_ >= 0) {
        ss << " @(" << targetRow_ << "," << targetCol_ << ")";
    }

    return ss.str();
}

std::unique_ptr<MCTSAction> ShardokAction::clone() const {
    return std::make_unique<ShardokAction>(
            commandIndex_,
            type_,
            player_,
            actorId_,
            targetRow_,
            targetCol_,
            hasOdds_);
}

bool ShardokAction::equals(const MCTSAction& other) const {
    const auto* shardokOther = dynamic_cast<const ShardokAction*>(&other);
    if (!shardokOther) { return false; }

    // Compare by index only - actions from same command list are uniquely identified by index
    return commandIndex_ == shardokOther->commandIndex_;
}

bool ShardokAction::requiresChanceNode() const {
    // Actions with probabilistic outcomes require chance nodes:
    // 1. Binary success/failure actions (hasOdds_): START_FIRE, FEAR, etc.
    // 2. END_TURN: random effects (fire spread, weather changes)
    // 3. Combat actions: roll affects damage dealt (MELEE, ARCHERY, CHARGE, DUEL)
    if (hasOdds_) { return true; }

    using namespace net::eagle0::shardok::common;
    switch (type_) {
        case END_TURN_COMMAND:
        case MELEE_COMMAND:
        case ARCHERY_COMMAND:
        case CHARGE_COMMAND:
        case CHALLENGE_DUEL_COMMAND:
        case REDUCE_COMMAND: return true;
        default: return false;
    }
}

}  // namespace shardok::mcts