// request_scheduler.hpp
#pragma once
#include <atomic>
#include <functional>
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
#include <taskflow/taskflow.hpp>

namespace lsp::scheduler
{
    class RequestScheduler
    {
    public:
        using TaskFunc = std::function<std::optional<std::string>()>;
        using ResponseCallback = std::function<void(const std::string&)>;
        
        explicit RequestScheduler(size_t concurrency = std::thread::hardware_concurrency());
        ~RequestScheduler();

        void Submit(const std::string& request_id, TaskFunc task);
        bool Cancel(const std::string& request_id);
        void SetResponseCallback(ResponseCallback callback);
        void WaitAll();

    private:
        struct TaskContext
        {
            std::atomic<bool> cancelled{false};
        };

        void SendResponse(const std::string& response);

    private:
        tf::Executor executor_;
        mutable std::mutex mutex_;
        std::unordered_map<std::string, std::shared_ptr<TaskContext>> running_tasks_;
        ResponseCallback response_callback_;
    };
}
