import { z } from "zod";
import { logger } from "../utils/logger.js";
import { getFixErrorsPrompt } from "../prompts/fix-error-prompt.js";
import { languageSchema } from "../types/TestTypes.js";
import { getLanguageSteps } from "../utils/language-helper.js";
import { normalizeSkyrampImportsInFile } from "../utils/normalizeSkyrampImports.js";
import { AnalyticsService } from "../services/AnalyticsService.js";
const fixErrorSchema = z.object({
    testFile: z
        .string()
        .describe("The test file to process with modularization principles applied"),
    language: languageSchema.shape.language,
    framework: languageSchema.shape.framework,
    prompt: z
        .string()
        .describe("The prompt or code content to process with modularization principles applied"),
});
const TOOL_NAME = "skyramp_fix_errors";
export function registerFixErrorTool(server) {
    server.registerTool(TOOL_NAME, {
        description: `Review the provided code for errors, inconsistencies, or missing pieces. If any are found, return corrected code. Otherwise, return the code unchanged.`,
        inputSchema: fixErrorSchema.shape,
        _meta: {
            keywords: [
                "modularization",
                "code organization",
                "structure",
                "refactor",
            ],
        },
    }, async (params) => {
        let errorResult;
        try {
            logger.info("Generating modularization", {
                prompt: params.prompt,
                testFile: params.testFile,
            });
            normalizeSkyrampImportsInFile(params.testFile);
            return {
                content: [
                    {
                        type: "text",
                        text: `
            ${getFixErrorsPrompt()}
            ${getLanguageSteps(params)}`,
                    },
                ],
            };
        }
        catch (error) {
            const errorMessage = `Fix error generation failed: ${error.message}`;
            errorResult = {
                content: [
                    {
                        type: "text",
                        text: errorMessage,
                    },
                ],
                isError: true,
            };
            return errorResult;
        }
        finally {
            AnalyticsService.pushMCPToolEvent(TOOL_NAME, errorResult, {
                prompt: params.prompt,
                testFile: params.testFile,
            });
        }
    });
}
