import { ResourceTemplate, } from "@modelcontextprotocol/sdk/server/mcp.js";
import { logger } from "../utils/logger.js";
import { AnalyticsService } from "../services/AnalyticsService.js";
import { MAX_TESTS_TO_GENERATE, MAX_RECOMMENDATIONS, MAX_CRITICAL_TESTS, } from "../prompts/test-recommendation/recommendationSections.js";
import { getTestbotPrompt, readWorkspaceServices } from "../prompts/testbot/testbot-prompts.js";
export function registerTestbotResource(server) {
    logger.info("Registering testbot resource");
    // RFC 6570 {+rest} (reserved expansion) captures the entire query string
    // including the leading "?". This avoids the SDK's per-param regex which
    // fails on empty query-param values (e.g. prDescription=).
    // We then parse query params from the URL object which handles URL-decoding
    // and empty values correctly.
    const template = new ResourceTemplate("skyramp://prompts/testbot{+rest}", {
        list: undefined,
    });
    server.registerResource("skyramp_testbot", template, {
        title: "Skyramp TestBot Prompt",
        description: "Returns task instructions for PR test analysis, generation, and maintenance.",
        mimeType: "text/plain",
    }, async (uri) => {
        const param = (name, fallback) => uri.searchParams.get(name) ?? fallback;
        const maxRec = parseInt(uri.searchParams.get("maxRecommendations") || "", 10);
        const maxGen = parseInt(uri.searchParams.get("maxGenerate") || "", 10);
        const prNum = parseInt(uri.searchParams.get("prNumber") || "", 10);
        const maxCrit = parseInt(uri.searchParams.get("maxCritical") || "", 10);
        const repositoryPath = param("repositoryPath", ".");
        const services = await readWorkspaceServices(repositoryPath);
        const prompt = getTestbotPrompt(param("prTitle", ""), param("prDescription", ""), param("summaryOutputFile", ""), repositoryPath, uri.searchParams.get("baseBranch") || undefined, isNaN(maxRec) ? MAX_RECOMMENDATIONS : maxRec, isNaN(maxGen) ? MAX_TESTS_TO_GENERATE : maxGen, isNaN(maxCrit) ? MAX_CRITICAL_TESTS : maxCrit, isNaN(prNum) ? undefined : prNum, uri.searchParams.get("userPrompt") || undefined, services.length ? services : undefined, uri.searchParams.get("stateOutputFile") || undefined, uri.searchParams.get("uiCredentials") || undefined, uri.searchParams.get("testsRepoDir") || undefined);
        AnalyticsService.pushMCPToolEvent("skyramp_testbot_prompt", undefined, {}).catch(() => { });
        // Return the original URI — clients may use it to re-fetch the resource,
        // and the caller already has these params. Credentials never appear in
        // the rendered prompt text (they are base64-encoded inside the
        // <ui-credentials> block; see getTestbotPrompt).
        return {
            contents: [
                {
                    uri: uri.toString(),
                    mimeType: "text/plain",
                    text: prompt,
                },
            ],
        };
    });
}
