package("cxxmcp")
    set_homepage("https://caomengxuan666.github.io/cxxmcp")
    set_description("C++ MCP SDK for protocol, client, server, transport, peer, and handler APIs.")
    set_license("MIT")

    add_urls("https://github.com/caomengxuan666/cxxmcp/releases/download/$(version)/cxxmcp-sdk-source-$(version).tar.gz")
    add_versions("v1.1.6", "7d321ee1e48f71666659949c98b5e3c81b273db98607b21d7b3d9301f5bf42e2")
    add_configs("http", {description = "Build HTTP/SSE transport (requires cpp-httplib).", default = false, type = "boolean"})
    add_configs("websocket", {description = "Build WebSocket transport (implies HTTP/cpp-httplib support).", default = false, type = "boolean"})
    add_configs("auth", {description = "Build the optional OAuth 2.1 / DPoP auth scaffold target.", default = false, type = "boolean"})

    add_deps("cmake")

    on_load(function (package)
        package:add("links", "mcp_server", "mcp_client", "mcp_protocol")
        local http_enabled = package:config("http") or package:config("websocket")
        if http_enabled then
            package:add("defines", "CXXMCP_ENABLE_HTTP=1")
        end
        if package:config("websocket") then
            package:add("defines", "CXXMCP_ENABLE_WEBSOCKET=1")
        end
        if package:config("auth") then
            package:add("defines", "CXXMCP_ENABLE_AUTH=1")
        end
        if package:is_plat("windows") and http_enabled then
            package:add("syslinks", "ws2_32")
            package:add("syslinks", "crypt32")
        elseif package:is_plat("linux", "bsd") then
            package:add("syslinks", "pthread")
        end
    end)

    on_install(function (package)
        if package:config("shared") then
            raise("cxxmcp currently supports static library linkage only")
        end
        local http_enabled = package:config("http") or package:config("websocket")
        local configs = {
            "-DCXXMCP_BUILD_SDK=ON",
            "-DCXXMCP_BUILD_EXAMPLES=OFF",
            "-DCXXMCP_BUILD_TESTS=OFF",
            "-DCXXMCP_BUILD_DOCS=OFF",
            "-DCXXMCP_ENABLE_HTTP=" .. (http_enabled and "ON" or "OFF"),
            "-DCXXMCP_ENABLE_WEBSOCKET=" .. (package:config("websocket") and "ON" or "OFF"),
            "-DCXXMCP_ENABLE_AUTH=" .. (package:config("auth") and "ON" or "OFF"),
            "-DCXXMCP_USE_SYSTEM_DEPS=OFF",
            "-DBUILD_SHARED_LIBS=OFF"
        }
        import("package.tools.cmake").install(package, configs)
    end)

    on_test(function (package)
        assert(package:check_cxxsnippets({test = [[
            #include <cstdint>
            #include <cxxmcp/protocol/serialization.hpp>
            void test() {
                auto request = mcp::protocol::make_ping_request(std::int64_t{1});
                auto serialized = mcp::protocol::serialize_request(request);
                (void)serialized;
            }
        ]]}, {configs = {languages = "c++17"}}))
        if package:config("auth") then
            assert(package:check_cxxsnippets({test = [[
                #include <cxxmcp/auth.hpp>
                void test() {
                    mcp::auth::ClientRegistrationOptions options;
                    options.redirect_uri = "http://localhost/callback";
                    auto request = mcp::auth::build_client_registration_request(options);
                    (void)request;
                }
            ]]}, {configs = {languages = "c++17"}}))
        end
    end)
