import axios from 'axios';
import path from 'path';
export class SecurityCommand {
    config;
    logger;
    constructor(config, logger) {
        this.config = config;
        this.logger = logger;
    }
    getMcpBaseUrl() {
        const fromEnv = process.env.VIGTHORIA_MCP_URL || process.env.MCP_SERVER_URL;
        if (fromEnv && fromEnv.trim()) {
            return fromEnv.replace(/\/$/, '');
        }
        return 'http://127.0.0.1:4008';
    }
    resolveDir(dir) {
        return path.resolve(dir || process.cwd());
    }
    async execute(tool, parameters) {
        const baseUrl = this.getMcpBaseUrl();
        const response = await axios.post(`${baseUrl}/mcp/execute`, {
            tool,
            parameters,
            context: {
                source: 'vigthoria-cli',
                client: 'vsec'
            }
        }, {
            timeout: 120000,
            validateStatus: (status) => status >= 200 && status < 500,
        });
        if (!response.data?.success) {
            const errorText = response.data?.error || `Tool ${tool} failed`;
            throw new Error(errorText);
        }
        const result = response.data?.result || {};
        return result.result || result;
    }
    async scan(options) {
        const dir = this.resolveDir(options.dir);
        const result = await this.execute('security_scan', { dir });
        if (options.json) {
            console.log(JSON.stringify(result, null, 2));
            return;
        }
        this.logger.info(`Security scan complete for: ${dir}`);
        this.logger.info(`Score: ${result.score}/100 (${result.grade})`);
        this.logger.info(`Files scanned: ${result.scannedFiles}`);
        this.logger.info(`Issues found: ${result.issueCount}`);
        const issues = Array.isArray(result.issues) ? result.issues : [];
        for (const issue of issues.slice(0, 20)) {
            console.log(`- [${issue.severity}] ${issue.id} ${issue.file}:${issue.line} ${issue.message}`);
        }
        if (issues.length > 20) {
            console.log(`... ${issues.length - 20} more issues`);
        }
    }
    async score(options) {
        const dir = this.resolveDir(options.dir);
        const result = await this.execute('security_score', { dir });
        if (options.json) {
            console.log(JSON.stringify(result, null, 2));
            return;
        }
        this.logger.info(`Security score for ${dir}`);
        this.logger.info(`${result.score}/100 (${result.grade})`);
        this.logger.info(result.summary || '');
    }
    async fix(options) {
        const dir = this.resolveDir(options.dir);
        const result = await this.execute('security_fix', {
            dir,
            issue_id: options.issueId,
            confirm: Boolean(options.apply),
        });
        if (options.json) {
            console.log(JSON.stringify(result, null, 2));
            return;
        }
        this.logger.info(`Security fix plan for ${dir}`);
        this.logger.info(result.message || 'Fix plan generated');
        const actions = Array.isArray(result.plannedActions) ? result.plannedActions : [];
        for (const action of actions.slice(0, 25)) {
            console.log(`- ${action.issue_id} ${action.file}: ${action.action}`);
        }
        if (actions.length > 25) {
            console.log(`... ${actions.length - 25} more actions`);
        }
    }
}
