//
// Debug test to understand why FrAdcData from FrameL files has empty RefData()
//

#include <iostream>
#include "framecpp/IFrameStream.hh"
#include "framecpp/FrameH.hh"
#include "framecpp/FrAdcData.hh"
#include "framecpp/FrVect.hh"
#include "framecpp/FrameCPP.hh"

int main()
{
    // Initialize FrameCPP to register all frame specifications
    FrameCPP::Initialize();

    bool framel_failed = false;
    bool framecpp_failed = false;

    const char* filename = "test_framel_v6.gwf";

    std::cout << "Opening FrameL v6 file: " << filename << std::endl;

    FrameCPP::IFrameFStream ifs(filename);

    std::cout << "File version: " << ifs.Version() << std::endl;

    // Test Method 1: ReadNextFrame (how test_framel_framecpp_compat does it)
    std::cout << "\n=== Method 1: ReadNextFrame() ===" << std::endl;
    auto frame = ifs.ReadNextFrame();
    if (frame && frame->GetRawData() && !frame->GetRawData()->RefFirstAdc().empty())
    {
        auto frame_adc = frame->GetRawData()->RefFirstAdc()[0];
        std::cout << "Frame read successfully" << std::endl;
        std::cout << "  FrAdcData name: " << frame_adc->GetName() << std::endl;
        std::cout << "  FrAdcData RefData() size: " << frame_adc->RefData().size() << std::endl;

        if (!frame_adc->RefData().empty())
        {
            std::cout << "  ✓ SUCCESS: ReadNextFrame() loads RefData correctly!" << std::endl;
        }
        else
        {
            std::cout << "  ✗ FAIL: ReadNextFrame() also has empty RefData()" << std::endl;
        }
    }

    // Test Method 2: ReadFrAdcData (should load recursively)
    std::cout << "\n=== Method 2: ReadFrAdcData(Recursively=true) ===" << std::endl;
    // Reopen file for fresh test
    FrameCPP::IFrameFStream ifs2(filename);
    auto adc_data = ifs2.ReadFrAdcData(0, "TestADC");

    if (!adc_data)
    {
        std::cerr << "ERROR: ReadFrAdcData returned null!" << std::endl;
        return 1;
    }

    std::cout << "FrAdcData read successfully" << std::endl;
    std::cout << "  Name: " << adc_data->GetName() << std::endl;
    std::cout << "  Sample rate: " << adc_data->GetSampleRate() << std::endl;
    std::cout << "  RefData() size: " << adc_data->RefData().size() << std::endl;

    if (adc_data->RefData().empty())
    {
        std::cerr << "\nERROR: RefData() is EMPTY despite Recursively=true!" << std::endl;
        std::cerr << "This is the bug we're investigating!" << std::endl;
        framel_failed = true;
    }
    else
    {
        std::cout << "\n✓ SUCCESS: FrameL file - RefData() has " << adc_data->RefData().size() << " FrVect objects" << std::endl;
        auto vect = adc_data->RefData()[0];
        if (vect)
        {
            std::cout << "  First FrVect name: " << vect->GetName() << std::endl;
        }
    }

    // Now test with a frameCPP-written file to see if the problem is specific to FrameL
    std::cout << "\n=== Testing frameCPP-written v6 file ===" << std::endl;
    const char* framecpp_file = "Z-framecpp_sample_downconvert_ver_6-600000000-1.gwf";

    try
    {
        FrameCPP::IFrameFStream ifs2(framecpp_file);
        std::cout << "Opening frameCPP v6 file: " << framecpp_file << std::endl;
        std::cout << "File version: " << ifs2.Version() << std::endl;

        auto adc_data2 = ifs2.ReadFrAdcData(0, "H1:TEST_ADC");
        if (adc_data2)
        {
            std::cout << "FrAdcData read successfully" << std::endl;
            std::cout << "  RefData() size: " << adc_data2->RefData().size() << std::endl;

            if (adc_data2->RefData().empty())
            {
                std::cerr << "ERROR: frameCPP file ALSO has empty RefData()!" << std::endl;
                framecpp_failed = true;
            }
            else
            {
                std::cout << "✓ frameCPP file - RefData() has " << adc_data2->RefData().size() << " FrVect objects" << std::endl;
            }
        }
    }
    catch (const std::exception& e)
    {
        std::cout << "Could not test frameCPP file: " << e.what() << std::endl;
    }

    if (framel_failed)
    {
        std::cout << "\n=== CONCLUSION ===" << std::endl;
        if (framecpp_failed)
        {
            std::cout << "RefData() is empty for BOTH FrameL and frameCPP files" << std::endl;
            std::cout << "This suggests a general problem with recursive loading, not FrameL-specific." << std::endl;
        }
        else
        {
            std::cout << "RefData() is empty ONLY for FrameL files" << std::endl;
            std::cout << "This suggests FrameL files have different PTR_STRUCT references or structure." << std::endl;
        }
        return 1;
    }

    return 0;
}
