#include "core/app_about.h"

#include "app_base.h"
#include "app_modules.h"
#include "imgui.h"

#include <string>

namespace fn_ui {

namespace {

std::string g_project     = "fn_registry app";
std::string g_version     = "";
std::string g_description = "";
bool        g_open        = false;

} // namespace

void about_window_set_info(const char* project,
                           const char* version,
                           const char* description) {
    if (project)     g_project     = project;
    if (version)     g_version     = version;
    if (description) g_description = description;
}

bool about_window_is_open()        { return g_open; }
void about_window_set_open(bool v) { g_open = v; }
void about_window_toggle()         { g_open = !g_open; }

bool about_window_menu_item(const char* label) {
    if (ImGui::MenuItem(label)) {
        g_open = true;
        return true;
    }
    return false;
}

void about_window_render() {
    if (!g_open) return;

    ImGui::SetNextWindowSize(ImVec2(420, 220), ImGuiCond_FirstUseEver);
    if (!ImGui::Begin("About", &g_open, ImGuiWindowFlags_NoCollapse)) {
        ImGui::End();
        return;
    }

    ImGui::TextUnformatted(g_project.c_str());

    if (!g_version.empty()) {
        ImGui::SameLine();
        ImGui::TextDisabled("v%s", g_version.c_str());
    }

    if (!g_description.empty()) {
        ImGui::Spacing();
        ImGui::Separator();
        ImGui::Spacing();
        ImGui::TextWrapped("%s", g_description.c_str());
    }

    // --- Modules consumidos por la app (issue 0097) ---
    // codegen_app_modules.py auto-prepende `framework_cpp` a uses_modules de
    // toda app C++, asi que la tabla Modules SIEMPRE lista al framework con
    // su version + cada modulo declarado en app.md::uses_modules.
    if (fn::app_modules_count > 0) {
        ImGui::Spacing();
        ImGui::Separator();
        ImGui::Spacing();
        ImGui::Text("Modules (%lu)", fn::app_modules_count);
        if (ImGui::BeginTable("##fn_modules_table", 2,
                              ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersInnerH |
                              ImGuiTableFlags_SizingStretchProp)) {
            ImGui::TableSetupColumn("Name",    ImGuiTableColumnFlags_WidthFixed, 140.0f);
            ImGui::TableSetupColumn("Version", ImGuiTableColumnFlags_WidthFixed,  80.0f);
            for (unsigned long i = 0; i < fn::app_modules_count; ++i) {
                const auto& m = fn::app_modules_array[i];
                if (!m.name) continue;
                ImGui::TableNextRow();
                ImGui::TableSetColumnIndex(0);
                ImGui::TextUnformatted(m.name);
                if (m.description && *m.description && ImGui::IsItemHovered()) {
                    ImGui::SetTooltip("%s", m.description);
                }
                ImGui::TableSetColumnIndex(1);
                ImGui::TextDisabled("v%s", m.version ? m.version : "?");
            }
            ImGui::EndTable();
        }
    }

    ImGui::Spacing();
    ImGui::Separator();
    ImGui::TextDisabled("fn_registry");

    ImGui::End();
}

} // namespace fn_ui
