#include "model_detector.h"
#include <iostream>
#include <vector>
#include <filesystem>

namespace fs = std::filesystem;

int main() {
    std::cout << "🧪 Simple Qwen Detection Test" << std::endl;
    std::cout << "============================" << std::endl;

    // Test with available Qwen models
    std::vector<std::string> qwenModelPaths = {
        "/data/SD_MODELS/diffusion_models/Qwen-Image-Edit-2509-Q3_K_S.gguf",
        "/data/SD_MODELS/diffusion_models/Qwen-Image-Pruning-13b-Q4_0.gguf",
        "/data/SD_MODELS/diffusion_models/qwen-image-Q2_K.gguf"
    };

    int successCount = 0;
    int totalTests = 0;

    for (const auto& modelPath : qwenModelPaths) {
        if (fs::exists(modelPath)) {
            totalTests++;
            std::cout << "\n=== Testing: " << fs::path(modelPath).filename().string() << " ===" << std::endl;
            
            try {
                ModelDetectionResult result = ModelDetector::detectModel(modelPath);
                
                std::cout << "📋 Detection Results:" << std::endl;
                std::cout << "  Architecture: " << result.architectureName << std::endl;
                std::cout << "  Architecture Enum: " << static_cast<int>(result.architecture) << std::endl;
                
                // Check if it's correctly detected as QWEN2VL
                bool isCorrectlyDetected = (result.architecture == ModelArchitecture::QWEN2VL);
                std::cout << "  ✅ Correctly detected as QWEN2VL: " << (isCorrectlyDetected ? "YES" : "NO") << std::endl;
                
                if (isCorrectlyDetected) {
                    successCount++;
                    std::cout << "  🎉 SUCCESS!" << std::endl;
                } else {
                    std::cout << "  ❌ FAILED: Expected QWEN2VL but got " << result.architectureName << std::endl;
                }

                // Show some tensor names for verification
                std::cout << "  🔍 Sample tensors (first 5):" << std::endl;
                int count = 0;
                for (const auto& tensorName : result.tensorNames) {
                    if (count >= 5) break;
                    std::cout << "    " << (count + 1) << ". " << tensorName << std::endl;
                    count++;
                }
                
            } catch (const std::exception& e) {
                std::cout << "❌ Error during detection: " << e.what() << std::endl;
            }
        } else {
            std::cout << "\n⚠️  Skipping non-existent model: " << modelPath << std::endl;
        }
    }

    // Summary
    std::cout << "\n🎯 Test Summary:" << std::endl;
    std::cout << "  Total Qwen models tested: " << totalTests << std::endl;
    std::cout << "  Successfully detected as QWEN2VL: " << successCount << std::endl;
    std::cout << "  Success rate: " << (totalTests > 0 ? (successCount * 100 / totalTests) : 0) << "%" << std::endl;

    if (successCount == totalTests && totalTests > 0) {
        std::cout << "  🎉 ALL TESTS PASSED! Qwen detection fix is working correctly." << std::endl;
        std::cout << "  ✅ Qwen models are no longer misidentified as Stable Diffusion 1.5" << std::endl;
    } else if (totalTests > 0) {
        std::cout << "  ❌ Some tests failed. The fix may need adjustment." << std::endl;
    } else {
        std::cout << "  ⚠️  No Qwen models found to test." << std::endl;
    }

    std::cout << "\n🏁 Qwen Detection Test Complete!" << std::endl;

    return (successCount == totalTests) ? 0 : 1;
}