//
// Created by Dan Crosby on 2025-01-15.
//

#ifndef EAGLE0_PERFORMANCETESTGAMESTATEBUILDER_HPP
#define EAGLE0_PERFORMANCETESTGAMESTATEBUILDER_HPP

#include <flatbuffers/flatbuffers.h>

#include <memory>
#include <vector>

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

namespace shardok {

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

/**
 * Builder class for creating game states used in performance testing.
 * Provides utilities to set up specific test scenarios matching the Unity client's
 * "Perf" button configuration.
 */
class PerformanceTestGameStateBuilder {
public:
    /**
     * Initialize game settings from the default configuration files.
     * Must be called before creating game states.
     */
    static auto InitializeGameSettings() -> GameSettingsSPtr;

    /**
     * Create the standard "Perf" test configuration:
     * - Map: Alah
     * - 6 AI units with professions 1-6, all Heavy Infantry
     * - 6 Human units (minimal configuration)
     *
     * @param settings The game settings to use
     * @param defenderToggle If true, AI is defender; if false, AI is attacker
     * @return A GameStateW with the configured battle
     */
    static auto CreatePerfTestGameState(
            const GameSettingsSPtr& settings,
            bool defenderToggle = false) -> GameStateW;

    /**
     * Create a custom test configuration with specified parameters.
     *
     * @param settings The game settings to use
     * @param mapName Name of the map to load
     * @param aiUnitCount Number of AI units to create
     * @param humanUnitCount Number of human units to create
     * @param defenderToggle If true, AI is defender; if false, AI is attacker
     * @return A GameStateW with the configured battle
     */
    static auto CreateCustomTestGameState(
            const GameSettingsSPtr& settings,
            const std::string& mapName,
            int aiUnitCount,
            int humanUnitCount,
            bool defenderToggle) -> GameStateW;

private:
    // Helper functions for building game state components
    static auto
    AddPlayerInfo(flatbuffers::FlatBufferBuilder& fbb, int playerId, bool isDefender, int food)
            -> flatbuffers::Offset<net::eagle0::shardok::storage::fb::PlayerInfo>;

    static auto AddGenericUnit(
            PlayerId playerId,
            UnitId unitId,
            const net::eagle0::shardok::storage::fb::Coords& location,
            int profession,
            int battalionType,
            int startingPositionIndex = -1) -> net::eagle0::shardok::storage::fb::Unit;

    // Battalion type constants (matching Unity client)
    static constexpr int HEAVY_INFANTRY_BATTALION_TYPE = 4;
};

}  // namespace shardok

#endif  // EAGLE0_PERFORMANCETESTGAMESTATEBUILDER_HPP