/*
 * test_reaper_timeout — verifies the server's keepalive reaper (docs/protocol.md §7).
 *
 * Simulates a half-open connection: client B authenticates then goes completely silent
 * (no TCP traffic, no pings — the 15s ping interval far exceeds the test's 2s reaper
 * timeout). Client A stays alive by sending channel text every 500ms, which bumps its
 * last_seen on the server. After ~2s the reaper drops B: B's TCP connection is closed
 * (B sees VC_EVENT_DISCONNECTED) and A receives VC_EVENT_USER_LEFT for B (the Tier 1
 * LEFT-broadcast fires from close()).
 *
 * This catches the "ghost user forever" failure mode for half-open connections (NAT
 * timeout, wifi loss without RST, laptop sleep) that never produce a TCP EOF.
 */
#include <cstdio>

#ifdef VOICECAT_HAS_NET

#include <atomic>
#include <chrono>
#include <condition_variable>
#include <filesystem>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

#include "voicecat.h"
#include "server.h"
#include "db.h"

struct EventStore {
    std::mutex              mu;
    std::condition_variable cv;

    bool      auth_ok{false};
    uint32_t  self_user_id{0};
    bool      channel_list_received{false};
    bool      disconnected{false};

    std::vector<uint32_t> left_users;

    vc_client*  client{nullptr};
    const char* label{nullptr};
};

static void on_event(void* user, const vc_event* ev) {
    auto* s = static_cast<EventStore*>(user);
    std::lock_guard lk(s->mu);
    switch (ev->type) {
        case VC_EVENT_SERVER_IDENTITY:
            vc_confirm_server_identity(s->client, 1);
            break;
        case VC_EVENT_AUTH_RESULT:
            s->auth_ok      = (ev->result == VC_OK);
            s->self_user_id = ev->user_id;
            break;
        case VC_EVENT_CHANNEL_LIST:
            s->channel_list_received = true;
            break;
        case VC_EVENT_USER_LEFT:
            s->left_users.push_back(ev->user_id);
            break;
        case VC_EVENT_DISCONNECTED:
            s->disconnected = true;
            break;
        default:
            break;
    }
    s->cv.notify_all();
}

template <typename Pred>
static bool wait_for(EventStore& s, Pred pred, int timeout_ms) {
    auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
    std::unique_lock lk(s.mu);
    return s.cv.wait_until(lk, deadline, [&] { return pred(s); });
}

static int g_failures = 0;
#define CHECK(cond)                                                        \
    do {                                                                   \
        if (!(cond)) {                                                     \
            std::printf("FAIL: %s (%s:%d)\n", #cond, __FILE__, __LINE__); \
            ++g_failures;                                                  \
        }                                                                  \
    } while (0)

int main() {
    auto tmp = std::filesystem::temp_directory_path() /
               ("vctest_reaper_" + std::to_string(
                   std::chrono::steady_clock::now().time_since_epoch().count()));
    std::filesystem::create_directories(tmp);
    std::string data_dir = tmp.string();

    std::atomic<uint16_t> bound_port{0};
    std::mutex              ready_mu;
    std::condition_variable ready_cv;
    bool                    ready{false};

    voicecat::server::Config cfg;
    cfg.data_dir     = data_dir;
    cfg.bind_port    = 0;
    cfg.server_name  = "VoiceCat-ReaperTest";
    cfg.allow_guests = true;
    cfg.reaper_timeout_ms = 2000;  // 2s — drop sessions silent for this long
    cfg.reaper_sweep_ms   = 500;   // check every 500ms
    cfg.on_ready = [&](uint16_t p) {
        bound_port.store(p);
        { std::lock_guard lk(ready_mu); ready = true; }
        ready_cv.notify_all();
    };

    voicecat::server::Server server(cfg);
    std::thread server_thread([&] { server.run(); });

    {
        std::unique_lock lk(ready_mu);
        if (!ready_cv.wait_for(lk, std::chrono::seconds(10), [&] { return ready; })) {
            std::printf("FAIL: server did not become ready\n");
            server.stop();
            server_thread.join();
            std::filesystem::remove_all(tmp);
            return 1;
        }
    }
    uint16_t port = bound_port.load();

    auto make_client = [&](const char* label, const char* nick) -> EventStore* {
        auto* ev = new EventStore();
        ev->label = label;
        vc_callbacks cb{on_event, nullptr, ev};
        vc_config cfgx{label, "0.1", VC_LOG_OFF};
        ev->client = vc_client_create(&cfgx, cb);
        if (!ev->client) return nullptr;
        if (vc_connect(ev->client, "127.0.0.1", port) != VC_OK) return nullptr;
        if (vc_authenticate_guest(ev->client, nick) != VC_OK) return nullptr;
        return ev;
    };

    EventStore* evA = make_client("clientA", "Alpha");
    CHECK(evA != nullptr);
    CHECK(wait_for(*evA, [](EventStore& s) { return s.auth_ok; }, 8000));
    CHECK(wait_for(*evA, [](EventStore& s) { return s.channel_list_received; }, 3000));
    uint32_t a_uid = evA->self_user_id;
    CHECK(a_uid != 0);

    EventStore* evB = make_client("clientB", "Bravo");
    CHECK(evB != nullptr);
    CHECK(wait_for(*evB, [](EventStore& s) { return s.auth_ok; }, 8000));
    CHECK(wait_for(*evB, [](EventStore& s) { return s.channel_list_received; }, 3000));
    uint32_t b_uid = evB->self_user_id;
    CHECK(b_uid != 0);

    // A stays alive by sending channel text every 500ms (bumps A's last_seen on the server).
    // B goes completely silent — no TCP traffic, no pings (15s ping >> 2s reaper timeout).
    // Start the keepalive IMMEDIATELY: the reaper timeout is only 2s, so A must begin
    // sending well before its last_seen goes stale.
    std::atomic<bool> keepalive_stop{false};
    std::thread keepalive([&] {
        while (!keepalive_stop.load()) {
            vc_send_text(evA->client, VC_TEXT_CHANNEL, 1, ".");
            std::this_thread::sleep_for(std::chrono::milliseconds(500));
        }
    });

    // Give B time to go stale and the reaper to fire (2s timeout + 500ms sweep + margin).
    // A must receive VC_EVENT_USER_LEFT for B.
    CHECK(wait_for(*evA, [b_uid](EventStore& s) {
        for (auto u : s.left_users) if (u == b_uid) return true;
        return false;
    }, 10000));

    // B's TCP connection is closed by the reaper → B sees VC_EVENT_DISCONNECTED.
    CHECK(wait_for(*evB, [](EventStore& s) { return s.disconnected; }, 5000));

    // ── Cleanup ──────────────────────────────────────────────────────────────
    keepalive_stop.store(true);
    keepalive.join();

    vc_disconnect(evA->client);
    if (!evB->disconnected) vc_disconnect(evB->client);
    vc_client_destroy(evA->client);
    vc_client_destroy(evB->client);
    delete evA;
    delete evB;

    server.stop();
    server_thread.join();
    std::filesystem::remove_all(tmp);

    if (g_failures == 0) {
        std::printf("reaper_timeout: all checks passed\n");
        return 0;
    }
    std::printf("reaper_timeout: %d failure(s)\n", g_failures);
    return 1;
}

#else  // !VOICECAT_HAS_NET

int main() {
    std::printf("reaper_timeout: SKIP (VOICECAT_HAS_NET not defined)\n");
    return 0;
}

#endif  // VOICECAT_HAS_NET
