#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.main = main;
const team_1 = require("../team");
const team_definition_audit_1 = require("./team-definition-audit");
function printUsage() {
    console.log('Usage: dominds validate_team_def [<member-id>]');
    console.log('');
    console.log('Validate explicit toolset declarations in `.minds/team.yaml` against the current toolset registry and `.minds/mcp.yaml` declarations.');
    console.log('MCP-declared but currently unloaded toolsets are reported as `DEFERRED`; disabled MCP toolsets are reported as `DISABLED` when `enabled: false` is set in `.minds/mcp.yaml`.');
    console.log('');
    console.log('Exit codes:');
    console.log('  0  No hard definition errors (`OK` / `DEFERRED` / `DISABLED` only)');
    console.log('  2  Hard definition errors found (`INVALID` / `MISS`)');
    console.log('');
    console.log('Examples:');
    console.log('  dominds validate_team_def');
    console.log('  dominds validate_team_def mentor');
}
function parseArgs(args) {
    let memberId;
    for (const arg of args) {
        if (arg === '-h' || arg === '--help') {
            printUsage();
            process.exit(0);
        }
        if (arg.startsWith('-')) {
            throw new Error(`Unknown option '${arg}'.`);
        }
        if (!memberId) {
            memberId = arg;
            continue;
        }
        throw new Error(`Unexpected argument '${arg}'.`);
    }
    return { memberId };
}
function resolveTargetMemberIds(team, memberId) {
    if (memberId)
        return [memberId];
    const visibleIds = Object.values(team.members)
        .filter((member) => member.hidden !== true)
        .map((member) => member.id)
        .sort((a, b) => a.localeCompare(b));
    if (visibleIds.length > 0)
        return visibleIds;
    const fallback = team.getDefaultResponder();
    if (!fallback)
        throw new Error('No team members found.');
    return [fallback.id];
}
async function main(argv = process.argv.slice(2)) {
    let parsed;
    try {
        parsed = parseArgs(argv);
    }
    catch (err) {
        console.error('Error:', err instanceof Error ? err.message : String(err));
        printUsage();
        process.exit(1);
        return;
    }
    try {
        const team = await team_1.Team.load();
        const targetMemberIds = resolveTargetMemberIds(team, parsed.memberId);
        const report = (0, team_definition_audit_1.buildToolsetAuditReport)({
            team,
            targetMemberIds,
            mcp: await (0, team_definition_audit_1.readMcpDeclaredToolsets)(),
        });
        process.stdout.write('# Team Definition Validation\n');
        process.stdout.write('This command checks explicit toolset references in `.minds/team.yaml`. `DEFERRED` usually means the toolset is declared through MCP but is not currently loaded; `DISABLED` means the server is intentionally `enabled: false` and exposes zero tools until `mcp_restart` enables it.\n');
        (0, team_definition_audit_1.printToolsetAudit)(report, { heading: '## Definition Audit' });
        if ((0, team_definition_audit_1.hasHardToolsetAuditFailures)(report)) {
            process.exit(2);
        }
    }
    catch (err) {
        console.error('Error validating team definition:', err instanceof Error ? err.message : String(err));
        process.exit(1);
    }
}
if (require.main === module) {
    main().catch((err) => {
        console.error('Unhandled error:', err);
        process.exit(1);
    });
}
