// Smoke test (no GUI): compila y ejecuta brevemente las APIs nuevas del
// issue 0025 para validar que el wrapper PIMPL del text_editor y el
// file_watcher (inotify Linux / ReadDirectoryChangesW Win) enlazan.
//
// No abre ventana ImGui — solo crea / settea texto / lee / poll / destruye.

#include "core/text_editor.h"
#include "core/file_watcher.h"

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <chrono>

int main() {
    // ----- text_editor -----
    auto* ed = fn::text_editor_create(fn::CodeLang::GLSL);
    if (!ed) { std::fprintf(stderr, "text_editor_create returned null\n"); return 1; }

    fn::text_editor_set_text(ed, "void main(){}\n");
    const char* got = fn::text_editor_get_text(ed);
    std::printf("text_editor: get_text -> %zu bytes\n", got ? std::strlen(got) : 0u);

    if (fn::text_editor_is_dirty(ed)) {
        std::fprintf(stderr, "text_editor: dirty unexpected after set_text\n");
        return 1;
    }
    fn::text_editor_destroy(ed);

    // ----- file_watcher -----
    const char* path = "/tmp/fn_smoke_test.txt";
    std::remove(path);
    {
        FILE* f = std::fopen(path, "w"); std::fputs("init\n", f); std::fclose(f);
    }

    auto* fw = fn::file_watcher_create();
    if (!fw) { std::fprintf(stderr, "file_watcher_create returned null\n"); return 1; }

    if (!fn::file_watcher_add(fw, path)) {
        std::fprintf(stderr, "file_watcher_add failed: %s\n", fn::file_watcher_last_error(fw));
        // Aun asi continuamos: en CI sin inotify (raro) este test seria flaky.
    }

    // Modificar
    {
        FILE* f = std::fopen(path, "w"); std::fputs("changed\n", f); std::fclose(f);
    }
    std::this_thread::sleep_for(std::chrono::milliseconds(50));

    auto evs = fn::file_watcher_poll(fw);
    std::printf("file_watcher: %zu events\n", evs.size());
    for (auto& e : evs) {
        const char* kind = e.kind == fn::FileEvent::Modified ? "MOD"
                          : e.kind == fn::FileEvent::Created ? "NEW" : "DEL";
        std::printf("  [%s] %s\n", kind, e.path.c_str());
    }

    fn::file_watcher_destroy(fw);
    std::remove(path);
    std::printf("OK\n");
    return 0;
}
