#include <GLFW/glfw3.h>
#include <iostream>
#include <windows.h>

int main() {
    std::cout << "=== OpenGL Diagnostics ===" << std::endl;

    // Initialize GLFW
    if (!glfwInit()) {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return 1;
    }

    // Create a window
    GLFWwindow* window = glfwCreateWindow(100, 100, "Test", nullptr, nullptr);
    if (!window) {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return 1;
    }

    glfwMakeContextCurrent(window);

    // Query OpenGL info
    const GLubyte* vendor = glGetString(GL_VENDOR);
    const GLubyte* renderer = glGetString(GL_RENDERER);
    const GLubyte* version = glGetString(GL_VERSION);

    std::cout << "\nVendor:   " << (vendor ? (const char*)vendor : "Unknown") << std::endl;
    std::cout << "Renderer: " << (renderer ? (const char*)renderer : "Unknown") << std::endl;
    std::cout << "Version:  " << (version ? (const char*)version : "Unknown") << std::endl;

    // Check which GPU is being used via Windows API
    std::cout << "\n=== GPU Selection Info ===" << std::endl;
    if (renderer) {
        std::string renderer_str((const char*)renderer);
        if (renderer_str.find("NVIDIA") != std::string::npos) {
            std::cout << "✓ Using NVIDIA GPU (Good!)" << std::endl;
        } else if (renderer_str.find("AMD") != std::string::npos ||
                   renderer_str.find("Radeon") != std::string::npos) {
            std::cout << "⚠ Using AMD integrated GPU" << std::endl;
            std::cout << "  Try forcing NVIDIA GPU via Windows Graphics Settings" << std::endl;
        } else if (renderer_str.find("Intel") != std::string::npos) {
            std::cout << "⚠ Using Intel integrated GPU" << std::endl;
        } else if (renderer_str.find("Microsoft") != std::string::npos ||
                   renderer_str.find("GDI") != std::string::npos) {
            std::cout << "✗ Using Microsoft software renderer (BAD!)" << std::endl;
            std::cout << "  GPU drivers may not be properly installed" << std::endl;
        }
    }

    std::cout << "\nPress Enter to exit..." << std::endl;
    std::cin.get();

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}
