/**
 * @file minimal_weight_test.cpp
 * @brief Minimal test to verify weight allocation functions don't crash
 *
 * Basic smoke test for weight_alloc_trump0 function to ensure it completes
 * without segmentation faults on minimal input data.
 */

#include <gtest/gtest.h>
#include <iostream>
#include "heuristic_sorting/heuristic_sorting.hpp"
#include "heuristic_sorting/internal.hpp"
#include <api/dll.h>

/**
 * Minimal test to isolate the segfault issue
 */
class MinimalWeightTest : public ::testing::Test
{
protected:
    void SetUp() override
    {
        // Initialize the DDS system
        SetMaxThreads(0);
    }
};

TEST_F(MinimalWeightTest, BasicWeightAllocCall) {
    std::cout << "Creating basic position..." << std::endl;
    
    // Create a basic position structure
    Pos tpos;
    memset(&tpos, 0, sizeof(Pos));
    
    // Set minimal required data
    tpos.length[0][0] = 4;  // North has 4 spades
    tpos.length[1][0] = 3;  // East has 3 spades
    tpos.length[2][0] = 3;  // South has 3 spades
    tpos.length[3][0] = 3;  // West has 3 spades
    
    // Create some test moves
    MoveType moves[3] = {
        {0, 14, 0, 0},  // Ace of spades
        {0, 13, 0, 0},  // King of spades
        {0, 12, 0, 0}   // Queen of spades
    };
    
    // Create other required structures
    MoveType bestMove = {0, 14, 1, 0};
    MoveType bestMoveTT = {0, 13, 1, 0};
    RelRanksType thrp_rel[4] = {};
    
    std::cout << "Creating HeuristicContext..." << std::endl;
    
    // Create HeuristicContext using constructor syntax
    HeuristicContext context = {
        tpos,          // const Pos& tpos
        bestMove,      // const MoveType& bestMove  
        bestMoveTT,    // const MoveType& bestMoveTT
        thrp_rel,      // const RelRanksType* thrp_rel
        moves,         // MoveType* mply
        3,             // numMoves
        0,             // lastNumMoves
        0,             // trump (spades)
        0,             // suit
        nullptr,       // trackp
        0,             // curr_trick
        0,             // curr_hand
        0,             // lead_hand
        0              // lead_suit
    };
    
    std::cout << "Calling weight_alloc_trump0..." << std::endl;
    
    try {
        weight_alloc_trump0(context);
        std::cout << "SUCCESS: weight_alloc_trump0 completed without crash!" << std::endl;
        
        // Print results
        for (int i = 0; i < 3; i++) {
            std::cout << "Move " << i << ": suit=" << moves[i].suit 
                      << " rank=" << moves[i].rank 
                      << " weight=" << moves[i].weight << std::endl;
        }
        
        SUCCEED();
    } catch (...) {
        FAIL() << "weight_alloc_trump0 threw an exception";
    }
}
