// D:\Dev\Mosis\MosisService\designer\src\testing\ui_inspector.h
#pragma once

#include <RmlUi/Core.h>
#include <nlohmann/json.hpp>
#include <string>

namespace mosis::testing {

class UIInspector {
public:
    UIInspector(Rml::Context* context);
    ~UIInspector() = default;

    // Set current screen URL override (to handle stale document detection)
    void SetCurrentScreen(const std::string& screenUrl) { m_current_screen = screenUrl; }

    // Dump entire UI hierarchy to JSON
    nlohmann::json DumpHierarchy() const;

    // Save hierarchy to file
    bool SaveHierarchy(const std::string& path) const;

    // Find element by selector (basic CSS-like selector)
    Rml::Element* FindElement(const std::string& selector) const;

    // Check if element exists and is visible
    bool ElementExists(const std::string& selector) const;
    bool ElementVisible(const std::string& selector) const;

    // Get element bounds
    struct Bounds {
        float x, y, width, height;
    };
    Bounds GetElementBounds(const std::string& selector) const;

    // Get element text content
    std::string GetElementText(const std::string& selector) const;

    // Get element attribute
    std::string GetElementAttribute(const std::string& selector, const std::string& attr) const;

private:
    nlohmann::json ElementToJson(Rml::Element* element) const;
    Rml::Element* FindElementRecursive(Rml::Element* root, const std::string& selector) const;

    Rml::Context* m_context;
    std::string m_current_screen;  // Override for screen URL detection
};

} // namespace mosis::testing
