import { z } from "zod";
import { TestType } from "../../types/TestTypes.js";
import { AnalyticsService } from "../../services/AnalyticsService.js";
import { getContractProviderAssertionsPrompt } from "../../prompts/enhance-assertions/contractProviderAssertionsPrompt.js";
import { getIntegrationAssertionsPrompt } from "../../prompts/enhance-assertions/integrationAssertionsPrompt.js";
import { getUIAssertionsPrompt } from "../../prompts/enhance-assertions/uiAssertionsPrompt.js";
import { isTestbotEnabled } from "../../utils/featureFlags.js";
import { stageGeneratedPaths } from "../../utils/gitStaging.js";
const TOOL_NAME = "skyramp_enhance_assertions";
const TESTBOT_UI_CHECKS = `
### Additional Testbot-Specific Checks
- If no suitable selector exists in the generated file for an assertion you need to add, go back and call \`browser_assert\` on the live page to record it with a valid selector, then re-export and regenerate.
- **After executing a UI test that documents a bug from \`issuesFound\`**: if it passed when you expected it to fail, the assertions are too weak — add a stronger \`expect()\` that directly targets the buggy behavior. This counts as the single allowed retry under the 2-attempt cap — do NOT re-run more than once.
`;
const enhanceAssertionsSchema = {
    testFile: z
        .string()
        .describe("Absolute path to the generated or updated test file whose assertions must be enhanced. Pass the exact file you just created or modified."),
    testType: z
        .enum([TestType.INTEGRATION, TestType.CONTRACT, TestType.UI])
        .describe("The type of test whose assertions should be enhanced. Contract applies to provider contract tests only, not consumer contract tests."),
    enhanceType: z
        .enum(["generation", "maintenance"])
        .describe("The context of the enhancement. " +
        "Use 'generation' after generating a new test file (applies to every test function). " +
        "Use 'maintenance' during drift UPDATE (applies only to new or diff-affected functions)."),
};
export function registerEnhanceAssertionsTool(server) {
    server.registerTool(TOOL_NAME, {
        description: `Enhance assertions for a generated or updated test file.

### When to call
- After generating a new supported test file
- After updating an existing supported test file during maintenance`,
        inputSchema: enhanceAssertionsSchema,
    }, async (params) => {
        const { testFile, testType, enhanceType } = params;
        // Stage so testbot includes the generated files in its output commit.
        await stageGeneratedPaths(testFile);
        const enhanceCtx = enhanceType;
        let instructions;
        if (testType === TestType.UI) {
            instructions = getUIAssertionsPrompt(testFile, enhanceCtx);
            if (isTestbotEnabled()) {
                instructions += TESTBOT_UI_CHECKS;
            }
        }
        else if (testType === TestType.CONTRACT) {
            instructions = getContractProviderAssertionsPrompt(testFile, enhanceCtx);
        }
        else if (testType === TestType.INTEGRATION) {
            instructions = getIntegrationAssertionsPrompt(testFile, enhanceCtx);
        }
        else {
            throw new Error(`Unsupported testType for ${TOOL_NAME}: ${testType}`);
        }
        const result = {
            content: [
                {
                    type: "text",
                    text: instructions,
                },
            ],
            isError: false,
        };
        AnalyticsService.pushMCPToolEvent(TOOL_NAME, undefined, params).catch(() => { });
        return result;
    });
}
