/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
 */
#include <chrono>
#include <future>
#include <iostream>
#include <memory>
#include <string>

#include <nlohmann/json.hpp>

#include "mcp_client.h"
#include "mcp_log.h"
#include "mcp_type.h"

constexpr int REQUEST_TIMEOUT = 300;
constexpr char EXAMPLE_ENDPOINT[] = "http://localhost:8000/mcp";
constexpr char EXAMPLE_TOKEN[] = "your-token";

int main(int argc, char** argv)
{
    SetLogLevel(MCP_LOG_LEVEL_INFO);

    std::string endpoint = EXAMPLE_ENDPOINT;
    for (int i = 1; i < argc; ++i) {
        const std::string arg = argv[i] ? std::string(argv[i]) : std::string{};
        if (arg.rfind("--port=", 0) == 0) {
            const std::string value = arg.substr(std::string("--port=").size());
            int port = std::stoi(value);
            endpoint = std::string("http://localhost:") + std::to_string(port) + "/mcp";
        }
    }

    // Setup client configuration
    Mcp::ClientConfig config;
    config.name = "ResourceExampleClient";
    config.version = "1.0.0";
    Mcp::StreamableHttpClientConfig httpConfig;
    httpConfig.endpoint = endpoint;
    httpConfig.tlsConfig.enabled = false;

    auto authProvider = std::make_shared<Mcp::BearerTokenProvider>(EXAMPLE_TOKEN);
    auto mcpClient = Mcp::McpClientFactory::CreateStreamableHttpClient(config, httpConfig, std::move(authProvider));

    // Initialize client
    MCP_LOG(MCP_LOG_LEVEL_INFO, "=== Example Initialize ===");
    try {
        auto initFuture = mcpClient->Initialize();
        if (initFuture.wait_for(std::chrono::seconds(REQUEST_TIMEOUT)) != std::future_status::ready) {
            MCP_LOG(MCP_LOG_LEVEL_ERROR, "Initialize timeout");
            return -1;
        }
        initFuture.get();
        MCP_LOG(MCP_LOG_LEVEL_INFO, "Initialize success");
    } catch (const std::exception &e) {
        MCP_LOG(MCP_LOG_LEVEL_ERROR, std::string("Initialize failed: ") + e.what());
        return -1;
    }

    // Example 1: List resources (paginated)
    MCP_LOG(MCP_LOG_LEVEL_INFO, "=== Example ListResources (paginated) ===");
    std::string targetUri;
    try {
        std::optional<std::string> cursor;
        std::size_t totalCount = 0;
        bool firstPage = true;

        do {
            auto listFuture = mcpClient->ListResources(cursor);
            if (listFuture.wait_for(std::chrono::seconds(REQUEST_TIMEOUT)) != std::future_status::ready) {
                MCP_LOG(MCP_LOG_LEVEL_ERROR, "ListResources timeout");
                return -1;
            }
            auto resourceList = listFuture.get();
            MCP_LOG(MCP_LOG_LEVEL_INFO,
                    std::string("ListResources page fetched, resource count: ") +
                        std::to_string(resourceList->resources.size()));
            for (const auto &resource : resourceList->resources) {
                MCP_LOG(MCP_LOG_LEVEL_INFO, "  Resource: " + resource.name + " (uri: " + resource.uri + ")");
            }
            if (firstPage && !resourceList->resources.empty()) {
                targetUri = resourceList->resources.front().uri;
                firstPage = false;
            }
            totalCount += resourceList->resources.size();
            cursor = resourceList->nextCursor;
        } while (cursor.has_value());

        MCP_LOG(MCP_LOG_LEVEL_INFO,
                std::string("ListResources completed, total resource count: ") + std::to_string(totalCount));
    } catch (const std::exception &e) {
        MCP_LOG(MCP_LOG_LEVEL_ERROR, std::string("ListResources failed: ") + e.what());
        return -1;
    }

    // Example 2: Subscribe/Unsubscribe resource
    if (!targetUri.empty()) {
        MCP_LOG(MCP_LOG_LEVEL_INFO, "=== Example Subscribe/Unsubscribe Resource ===");
        try {
            auto subFuture = mcpClient->SubscribeResource(targetUri);
            if (subFuture.wait_for(std::chrono::seconds(REQUEST_TIMEOUT)) != std::future_status::ready) {
                MCP_LOG(MCP_LOG_LEVEL_ERROR, "SubscribeResource timeout");
                return -1;
            }
            subFuture.get();
            MCP_LOG(MCP_LOG_LEVEL_INFO, "SubscribeResource success: " + targetUri);

            auto unsubFuture = mcpClient->UnsubscribeResource(targetUri);
            if (unsubFuture.wait_for(std::chrono::seconds(REQUEST_TIMEOUT)) != std::future_status::ready) {
                MCP_LOG(MCP_LOG_LEVEL_ERROR, "UnsubscribeResource timeout");
                return -1;
            }
            unsubFuture.get();
            MCP_LOG(MCP_LOG_LEVEL_INFO, "UnsubscribeResource success: " + targetUri);
        } catch (const std::exception &e) {
            MCP_LOG(MCP_LOG_LEVEL_ERROR, std::string("Subscribe/Unsubscribe failed: ") + e.what());
            return -1;
        }
    }

    // Example 3: Read resource
    if (!targetUri.empty()) {
        MCP_LOG(MCP_LOG_LEVEL_INFO, "=== Example ReadResource ===");
        try {
            auto readFuture = mcpClient->ReadResource(targetUri);
            if (readFuture.wait_for(std::chrono::seconds(REQUEST_TIMEOUT)) != std::future_status::ready) {
                MCP_LOG(MCP_LOG_LEVEL_ERROR, "ReadResource timeout");
                return -1;
            }
            auto readResult = readFuture.get();
            MCP_LOG(MCP_LOG_LEVEL_INFO, "ReadResource success: " + targetUri + ", content count: " +
                std::to_string(readResult->contents.size()));
        } catch (const std::exception &e) {
            MCP_LOG(MCP_LOG_LEVEL_ERROR, std::string("ReadResource failed: ") + e.what());
            return -1;
        }
    }

    // Example 4: List resource templates
    MCP_LOG(MCP_LOG_LEVEL_INFO, "=== Example ListResourcesTemplates ===");
    try {
        auto templateFuture = mcpClient->ListResourcesTemplates();
        if (templateFuture.wait_for(std::chrono::seconds(REQUEST_TIMEOUT)) != std::future_status::ready) {
            MCP_LOG(MCP_LOG_LEVEL_ERROR, "ListResourcesTemplates timeout");
            return -1;
        }
        auto templates = templateFuture.get();
        MCP_LOG(MCP_LOG_LEVEL_INFO,
                std::string("ListResourcesTemplates success, template count: ") +
                    std::to_string(templates->resourceTemplates.size()));
        for (const auto &tmpl : templates->resourceTemplates) {
            std::string mimeType = tmpl.mimeType.has_value() ? tmpl.mimeType.value() : std::string("none");
            MCP_LOG(MCP_LOG_LEVEL_INFO, "  Template: " + tmpl.uriTemplate + " (mimeType: " + mimeType + ")");
        }
    } catch (const std::exception &e) {
        MCP_LOG(MCP_LOG_LEVEL_ERROR, std::string("ListResourcesTemplates failed: ") + e.what());
        return -1;
    }

    mcpClient->CloseGracefully();

    MCP_LOG(MCP_LOG_LEVEL_INFO, "=== Example completed ===");
    return 0;
}