/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 */

#ifndef MCP_CLIENT_SESSION_INCLUDE_H_
#define MCP_CLIENT_SESSION_INCLUDE_H_

#include <atomic>
#include <functional>
#include <future>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>

#include "mcp_error.h"
#include "mcp_client.h"
#include "mcp_type.h"
#include "shared/base_session.h"
#include "shared/jsonrpc.h"

namespace Mcp {
// Client session class for handling requests and notifications
class ClientSession : public BaseSession {
public:
    using BaseSession::SendNotification;
    using BaseSession::SendRequest;

    // Constructor with configurable client configuration (defaults provided)
    explicit ClientSession(std::shared_ptr<ClientTransport> transport,
                           const ClientConfig& clientConfig = ClientConfig(), std::string sessionId = "");

    // Initialize session with the server (send initialize request and wait response)
    std::future<std::shared_ptr<InitializeResult>> Initialize();

    // Send a ping request and wait for a response
    std::future<std::shared_ptr<EmptyResult>> SendPing();

    // Send a roots list changed notification
    void SendRootsListChanged();

    // Set the callback for handling roots/list requests from the server
    void SetListRootsCallback(ListRootsCallback cb);

    // Set the callback for handling elicitation/create requests from the server
    void SetElicitCallback(ElicitCallback cb);

    // Set the callback for handling elicitation/create requests from the server
    void SetElicitUrlCallback(ElicitUrlCallback cb);

    // Set the callback for handling notifications/message from the server
    void SetLoggingCallback(LoggingCallback cb);

    // Set the callback for handling sampling/createMessage requests from the server.
    // Must be set before Initialize() so the client advertises sampling support.
    void SetSamplingCreateMessageCallback(McpClient::SamplingCreateMessageCallback cb,
        SamplingCapability capability = SamplingCapability{});

    // Set the logging level
    std::future<std::shared_ptr<EmptyResult>> SetLoggingLevel(LoggingLevel level);

    // Call a tool on the server (optional progressCallback enables progressToken and notifications/progress).
    std::future<std::shared_ptr<CallToolResult>> CallTool(const std::string& name,
                                                          const std::optional<std::string>& arguments = std::nullopt,
                                                          int timeout = 0,
                                                          std::optional<ProgressCallback> progressCallback
                                                              = std::nullopt);

    // List available tools. When cursor is provided, the server will return
    // a page of tools starting from that cursor and may include nextCursor in
    // the result for subsequent pages.
    std::future<std::shared_ptr<ListToolsResult>> ListTools();
    std::future<std::shared_ptr<ListToolsResult>> ListTools(const std::optional<std::string>& cursor);

    // List prompts
    std::future<std::shared_ptr<ListPromptsResult>> ListPrompts();

    // --- Resources ---
    std::future<std::shared_ptr<ListResourcesResult>> ListResources();
    std::future<std::shared_ptr<ListResourcesResult>> ListResources(const std::optional<std::string>& cursor);
    std::future<std::shared_ptr<ListResourceTemplatesResult>> ListResourcesTemplates();
    std::future<std::shared_ptr<ReadResourceResult>> ReadResource(const std::string& uri);
    std::future<std::shared_ptr<EmptyResult>> SubscribeResource(const std::string& uri);
    std::future<std::shared_ptr<EmptyResult>> UnsubscribeResource(const std::string& uri);

    // Get prompt from server
    std::future<std::shared_ptr<GetPromptResult>> GetPrompt(const std::string& name,
                                                            const std::optional<JsonValue>& arguments = std::nullopt);

    // Request completion options
    std::future<std::shared_ptr<CompleteResult>> Complete(const CompleteReference& ref,
                                                          const CompletionArgument& argument,
                                                          const std::optional<CompletionContext>& context
                                                            = std::nullopt);

    // Override BaseSession notification sending
    void SendNotification(std::unique_ptr<Notification> notification,
                          std::optional<RequestId> relatedRequestId = std::nullopt) override;

    // Check if initialize has completed successfully
    bool IsInitialized() const
    {
        return initialized_;
    }

    // Get the capabilities advertised by the connected server.
    // Populated when the client receives the InitializeResult.
    ServerCapabilities GetServerCapabilities() const;

    // Send notifications/initialized after successful initialize handshake
    void SendInitializedNotification();

    /** Send notifications/progress to the server (MCP progress tracking). progressToken is string or int64_t. */
    void SendProgressNotification(ProgressToken progressToken, double progress,
                                  std::optional<double> total = std::nullopt,
                                  const std::optional<std::string>& message = std::nullopt) override;

protected:
    /**
    * @brief Handle an incoming JSON-RPC notification from the server.
    *
    * This is called by `BaseSession` when a notification message is parsed.
    */
    void ReceivedNotification(const Notification& notification) override;

    void ReceivedRequest(const RequestId& requestId, const Request& request, RequestContext& ctx) override;

private:
    // Incremental request ID for tracking (thread-safe)
    std::atomic<int64_t> _request_id{1};

    // Pending requests map: id -> raw-response callback
    std::mutex pending_mutex_;
    std::unordered_map<int64_t, std::function<void(const std::string&)>> pending_callbacks_;

    // Transport callback that sends raw JSON strings. Must be set by caller
    // (or via constructor) before making requests.
    std::function<bool(const std::string&)> send_raw_;

    // Build client capabilities for initialize request.
    ClientCapabilities BuildClientCapabilities() const;

    // Handle roots/list request. This is used when the server asks the client to
    // provide its roots. The request is only supported when listRootsCallback_ is set.
    void HandleRootsListRequest(const RequestId& requestId, const Request& request, RequestContext& ctx);

    void HandleElicitRequest(const RequestId& requestId, const Request& request, RequestContext& ctx);

    // Handle sampling/createMessage server-initiated request.
    void HandleSamplingCreateMessageRequest(const RequestId& requestId, const Request& request, RequestContext& ctx);

    // Utility helpers for replying to server-initiated requests with JSON-RPC errors.
    // These helpers centralize error formatting and ensure consistent responses.
    void SendJsonRpcError(const RequestId& requestId, JsonRpcErrorCode code, const std::string& message,
                          RequestContext& ctx);
    void SendJsonRpcErrorRaw(const RequestId& requestId, int code, const std::string& message, RequestContext& ctx);
    void SendMethodNotFound(const RequestId& requestId, const std::string& method, RequestContext& ctx);
    void SendInternalError(const RequestId& requestId, const std::string& message, RequestContext& ctx);

    /** Handle notifications/progress: look up callback by progressToken (string or int64_t) and invoke it. */
    void HandleProgressNotification(const ProgressToken& progressToken, double progress,
                                    std::optional<double> total, const std::optional<std::string>& message);

    // Indicates whether initialize has succeeded
    bool initialized_{false};

    // Capabilities advertised by the server during the MCP handshake.
    std::optional<ServerCapabilities> serverCapabilities_{std::nullopt};

    // If set, the client supports roots/list. Used to decide whether to advertise
    // ClientCapabilities.roots in the initialize request.
    ListRootsCallback listRootsCallback_{nullptr};

    LoggingCallback loggingCallback_{nullptr};

    ElicitCallback elicitCallback_{nullptr};
    ElicitUrlCallback elicitUrlCallback_{nullptr};

    // If set, the client supports sampling/createMessage.
    McpClient::SamplingCreateMessageCallback samplingCreateMessageCallback_{nullptr};
    SamplingCapability samplingCapability_{};

    // Client configuration used to build initialize.clientInfo
    ClientConfig clientConfig_;

    // Cache for tool output schemas
    std::unordered_map<std::string, JsonValue> toolOutputSchemas_;

    // Cache schemas from ListTools result
    void CacheToolSchemas(const ListToolsResult& r);

    // Validate tool result against cached output schema
    void ValidateToolResult(const std::string& name, const CallToolResult& result);
};

} // namespace Mcp

#endif // MCP_CLIENT_SESSION_INCLUDE_H_