// Test to verify FrDetector consistency between dnt_init and FrDetectorCache

#include <iostream>
#include <string>
#include "framecpp/Universal/FrDetector.hh"
#include "framecpp/Common/DetectorNames.hh"

using namespace FrameCPP::Universal;

int main()
{
    // Test that we can access DetectorNameTable
    std::cout << "Testing DetectorNameTable access..." << std::endl;
    
    // Test known detectors
    const std::string detectors[] = {
        "ACIGA", "CIT_40", "GEO_600", "KAGRA", 
        "LHO_4k", "LIGO_India", "LLO_4k", "TAMA_300", "VIRGO"
    };
    
    const std::string prefixes[] = {
        "U1", "C1", "G1", "K1", "H1", "A1", "L1", "T1", "V1"
    };
    
    const FrDetector::dataQualityOffsets offsets[] = {
        FrDetector::DQO_ACIGA, FrDetector::DQO_CIT_40, FrDetector::DQO_GEO_600,
        FrDetector::DQO_KAGRA, FrDetector::DQO_LHO_4K, FrDetector::DQO_LIGO_INDIA,
        FrDetector::DQO_LLO_4K, FrDetector::DQO_TAMA_300, FrDetector::DQO_VIRGO
    };
    
    bool all_good = true;
    
    for (size_t i = 0; i < 9; ++i)
    {
        try {
            // Test GetDetector by offset
            auto detector = FrDetector::GetDetector(offsets[i]);
            if (!detector) {
                std::cerr << "ERROR: GetDetector(" << offsets[i] << ") returned null" << std::endl;
                all_good = false;
                continue;
            }
            
            // Test GetDetector by name
            auto detector2 = FrDetector::GetDetector(detectors[i]);
            if (!detector2) {
                std::cerr << "ERROR: GetDetector(\"" << detectors[i] << "\") returned null" << std::endl;
                all_good = false;
                continue;
            }
            
            // Verify they're the same
            if (detector.get() != detector2.get()) {
                std::cerr << "ERROR: GetDetector by offset and name return different objects for " 
                          << detectors[i] << std::endl;
                all_good = false;
            }
            
            // Test GetDataQualityOffset
            auto dqo = FrDetector::GetDataQualityOffset(detectors[i]);
            if (dqo != offsets[i]) {
                std::cerr << "ERROR: GetDataQualityOffset(\"" << detectors[i] 
                          << "\") returned " << dqo << " expected " << offsets[i] << std::endl;
                all_good = false;
            }
            
            // Test prefix lookup
            auto dqo2 = FrDetector::GetDataQualityOffset(prefixes[i]);
            if (dqo2 != offsets[i]) {
                std::cerr << "ERROR: GetDataQualityOffset(\"" << prefixes[i] 
                          << "\") returned " << dqo2 << " expected " << offsets[i] << std::endl;
                all_good = false;
            }
            
            std::cout << "✓ " << detectors[i] << " (" << prefixes[i] << ":" 
                      << ", DQO=" << offsets[i] << ")" << std::endl;
            
        } catch (const std::exception& e) {
            std::cerr << "ERROR: Exception for " << detectors[i] << ": " << e.what() << std::endl;
            all_good = false;
        }
    }
    
    if (all_good) {
        std::cout << "\nAll tests passed! FrDetector data structures are consistent." << std::endl;
        return 0;
    } else {
        std::cerr << "\nSome tests failed!" << std::endl;
        return 1;
    }
}