import { resolve } from 'node:path';
import { analyzeSchema } from '@pellux/goodvibes-sdk/platform/tools/analyze/schema';
import { appendSchemaFingerprint } from '@pellux/goodvibes-sdk/platform/tools/shared/schema-fingerprint';
import { applyAnalyzeTokenBudget, summarizeAnalyzeResult } from '@pellux/goodvibes-sdk/platform/tools/analyze/shared';
import { runBundle, runCoverage, runDeadCode, runDependencies, runEnvAudit, runImpact, runPermissions, runPreview, runSecurity, runSurface, runTestFind, } from '@pellux/goodvibes-sdk/platform/tools/analyze/scan-modes';
import { runBreaking, runDiff, runSemanticDiff, runUpgrade, } from './git-modes.js';
import { summarizeError } from '@pellux/goodvibes-sdk/platform/utils/error-display';
export function createAnalyzeTool(toolLLM, featureFlags, projectRoot) {
    if (typeof projectRoot !== 'string' || projectRoot.trim().length === 0) {
        throw new Error('createAnalyzeTool requires projectRoot');
    }
    return {
        definition: analyzeSchema,
        async execute(args) {
            try {
                if (!args.mode || typeof args.mode !== 'string') {
                    return { success: false, error: 'Missing required "mode" field' };
                }
                const input = args;
                const outputFormat = input.output?.format ?? 'json';
                const resolvedProjectRoot = resolve(typeof input.projectRoot === 'string' && input.projectRoot.trim().length > 0
                    ? input.projectRoot
                    : projectRoot);
                let result;
                switch (input.mode) {
                    case 'impact':
                        result = await runImpact(input, resolvedProjectRoot);
                        break;
                    case 'dependencies':
                        result = await runDependencies(input, resolvedProjectRoot);
                        break;
                    case 'dead_code':
                        result = await runDeadCode(input, resolvedProjectRoot);
                        break;
                    case 'security':
                        result = await runSecurity(input, resolvedProjectRoot);
                        break;
                    case 'coverage':
                        result = await runCoverage(input, resolvedProjectRoot);
                        break;
                    case 'bundle':
                        result = await runBundle(input, resolvedProjectRoot);
                        break;
                    case 'surface':
                        result = await runSurface(input, resolvedProjectRoot);
                        break;
                    case 'preview':
                        result = await runPreview(input, resolvedProjectRoot);
                        break;
                    case 'diff':
                        result = await runDiff(input, resolvedProjectRoot);
                        break;
                    case 'breaking':
                        result = await runBreaking(input, resolvedProjectRoot);
                        break;
                    case 'semantic_diff':
                        result = await runSemanticDiff(input, resolvedProjectRoot, toolLLM);
                        break;
                    case 'upgrade':
                        result = await runUpgrade(input, resolvedProjectRoot);
                        break;
                    case 'permissions':
                        result = await runPermissions(input, resolvedProjectRoot);
                        break;
                    case 'env_audit':
                        result = await runEnvAudit(input, resolvedProjectRoot);
                        break;
                    case 'test_find':
                        result = await runTestFind(input, resolvedProjectRoot);
                        break;
                    default: {
                        const exhaustive = input.mode;
                        return { success: false, error: `Unknown mode: ${exhaustive}` };
                    }
                }
                const fingerprinted = appendSchemaFingerprint(result, 'analyze', input.mode, { featureFlags });
                const shaped = outputFormat === 'summary'
                    ? summarizeAnalyzeResult(input.mode, fingerprinted)
                    : fingerprinted;
                const indent = outputFormat === 'json' ? 2 : 0;
                const serialized = JSON.stringify(shaped, null, indent);
                return { success: true, output: applyAnalyzeTokenBudget(serialized, input.output?.max_tokens) };
            }
            catch (err) {
                return {
                    success: false,
                    error: summarizeError(err),
                };
            }
        },
    };
}
