#pragma once
#include <string>
#include <spdlog/spdlog.h>
#include "../../protocol/protocol.hpp"
#include "../../scheduler/request_scheduler.hpp"

namespace lsp::providers
{
    enum class ServerLifecycleEvent
    {
        kInitializing,
        kInitialized,
        kInitializeFailed,
        kShuttingDown,
        kShutdown
    };

    using LifecycleCallback = std::function<void(ServerLifecycleEvent)>;

    class ProviderContext;

    // LSP请求提供者接口基类
    class IProvider
    {
    public:
        virtual ~IProvider() = default;

        // 获取支持的LSP方法名
        virtual std::string GetMethod() const = 0;
        // 获取提供者名称（用于日志和调试）
        virtual std::string GetProviderName() const = 0;
    };

    // LSP 请求处理器接口
    class IRequestProvider : public IProvider
    {
    public:
        virtual ~IRequestProvider() = default;

        // 处理LSP请求
        virtual std::string ProvideResponse(const protocol::RequestMessage& request, ProviderContext& context) = 0;
        static std::string BuildErrorResponseMessage(const protocol::RequestMessage& request, protocol::ErrorCode code, const std::string& message);
    };

    // LSP 通知处理器接口
    class INotificationProvider : public IProvider
    {
    public:
        virtual ~INotificationProvider() = default;

        // 处理LSP通知
        virtual void HandleNotification(const protocol::NotificationMessage& notification, ProviderContext& context) = 0;
    };

    class ProviderContext
    {
    public:
        ProviderContext(scheduler::RequestScheduler* scheduler, LifecycleCallback lifecycle_callback) :
            scheduler_(scheduler), lifecycle_callback_(lifecycle_callback) {}

        scheduler::RequestScheduler* GetScheduler() const { return scheduler_; }

        void TriggerLifecycleEvent(ServerLifecycleEvent event) const
        {
            if (lifecycle_callback_)
                lifecycle_callback_(event);
        }

    private:
        scheduler::RequestScheduler* scheduler_;
        LifecycleCallback lifecycle_callback_;
    };
}
