function text(value) {
    return { content: [{ type: 'text', text: JSON.stringify(value, null, 2) }] };
}
function resolveOperativeId(value) {
    const operativeId = String(value ?? process.env.SYNAPSE_OPERATIVE_ID ?? '').trim();
    return operativeId || null;
}
function booleanValue(value) {
    return value === true || String(value ?? '').toLowerCase() === 'true';
}
export const synapseToolDefinitions = [
    {
        name: 'nexus_synapse_mandate',
        description: 'Deploy a Synapse Strike Team from a top-level mandate.',
        inputSchema: {
            type: 'object',
            properties: {
                mandateText: { type: 'string' },
                budgetUsd: { type: 'number' },
                maxOperatives: { type: 'number' },
            },
            required: ['mandateText'],
        },
    },
    {
        name: 'nexus_synapse_hire',
        description: 'Manually create an operative.',
        inputSchema: {
            type: 'object',
            properties: {
                name: { type: 'string' },
                roleTitle: { type: 'string' },
                reportsToOperativeId: { type: 'string' },
                skillId: { type: 'string' },
                specialistId: { type: 'string' },
                budgetCapUsd: { type: 'number' },
                sortieIntervalMs: { type: 'number' },
                strikeTeamId: { type: 'string' },
                requestApproval: { type: 'boolean' },
                requestedByOperativeId: { type: 'string' },
            },
            required: [],
        },
    },
    {
        name: 'nexus_synapse_operatives',
        description: 'List operative roster with lifecycle and budget status.',
        inputSchema: {
            type: 'object',
            properties: {
                operativeId: { type: 'string' },
            },
            required: [],
        },
    },
    {
        name: 'nexus_synapse_operatives_update',
        description: 'Pause, resume, terminate, reassign, or update an operative profile.',
        inputSchema: {
            type: 'object',
            properties: {
                operativeId: { type: 'string' },
                action: { type: 'string', enum: ['pause', 'resume', 'terminate', 'reassign', 'profile'] },
                roleTitle: { type: 'string' },
                reportsToOperativeId: { type: 'string' },
                skillId: { type: 'string' },
                specialistId: { type: 'string' },
                budgetCapUsd: { type: 'number' },
                sortieIntervalMs: { type: 'number' },
                missionId: { type: 'string' },
                strikeTeamId: { type: 'string' },
                reason: { type: 'string' },
            },
            required: ['operativeId'],
        },
    },
    {
        name: 'nexus_synapse_heartbeat',
        description: 'Trigger a manual heartbeat/sortie for an operative.',
        inputSchema: {
            type: 'object',
            properties: {
                operativeId: { type: 'string' },
                force: { type: 'boolean' },
            },
            required: ['operativeId'],
        },
    },
    {
        name: 'nexus_synapse_approval_resolve',
        description: 'Approve or reject a pending Synapse approval request.',
        inputSchema: {
            type: 'object',
            properties: {
                approvalId: { type: 'string' },
                decision: { type: 'string', enum: ['approved', 'rejected'] },
            },
            required: ['approvalId', 'decision'],
        },
    },
    {
        name: 'nexus_synapse_assign_mission',
        description: 'Deploy a one-off mandate as a mission assignment.',
        inputSchema: {
            type: 'object',
            properties: {
                mandateText: { type: 'string' },
            },
            required: ['mandateText'],
        },
    },
    {
        name: 'nexus_synapse_sortie_start',
        description: 'Run a sortie for one operative.',
        inputSchema: { type: 'object', properties: { operativeId: { type: 'string' } }, required: [] },
    },
    {
        name: 'nexus_synapse_sortie_end',
        description: 'Alias for sortie completion reporting.',
        inputSchema: { type: 'object', properties: { operativeId: { type: 'string' } }, required: [] },
    },
    {
        name: 'nexus_synapse_field_report',
        description: 'Submit a field report payload.',
        inputSchema: { type: 'object', properties: { report: { type: 'object' } }, required: ['report'] },
    },
    {
        name: 'nexus_synapse_cost_report',
        description: 'Return basic cost telemetry for an operative.',
        inputSchema: { type: 'object', properties: { operativeId: { type: 'string' } }, required: ['operativeId'] },
    },
    {
        name: 'nexus_synapse_request_approval',
        description: 'Create an approval gate request.',
        inputSchema: {
            type: 'object',
            properties: {
                operativeId: { type: 'string' },
                missionId: { type: 'string' },
                scope: { type: 'string', enum: ['operative', 'mission', 'crew', 'project'] },
                scopeId: { type: 'string' },
                action: { type: 'string' },
                description: { type: 'string' },
            },
            required: ['action', 'description'],
        },
    },
    {
        name: 'nexus_synapse_echo',
        description: 'Query predecessor findings.',
        inputSchema: {
            type: 'object',
            properties: {
                missionId: { type: 'string' },
                missionTitle: { type: 'string' },
                currentOperativeId: { type: 'string' },
            },
            required: ['missionTitle'],
        },
    },
    {
        name: 'nexus_synapse_mission_progress',
        description: 'Persist a mission progress note.',
        inputSchema: {
            type: 'object',
            properties: {
                operativeId: { type: 'string' },
                missionId: { type: 'string' },
                note: { type: 'string' },
            },
            required: ['note'],
        },
    },
    {
        name: 'nexus_synapse_striketeam_status',
        description: 'Get strike team status.',
        inputSchema: { type: 'object', properties: { strikeTeamId: { type: 'string' } }, required: [] },
    },
    {
        name: 'nexus_synapse_health',
        description: 'Get operative health.',
        inputSchema: { type: 'object', properties: { operativeId: { type: 'string' } }, required: [] },
    },
];
export async function handleSynapseToolCall(name, args, synapse) {
    if (!name.startsWith('nexus_synapse_'))
        return null;
    if (!synapse)
        return text({
            error: 'synapse-disabled',
            reason: 'Synapse runtime was not initialized during server startup. The SQLite database may not be accessible from the current working directory.',
            remediation: [
                'Check server logs for "[Synapse] DB unavailable"',
                'Ensure better-sqlite3 native module is compiled: npm rebuild better-sqlite3',
                'Set SYNAPSE_ENABLED=false to explicitly disable and suppress this error',
            ],
        });
    switch (name) {
        case 'nexus_synapse_mandate':
        case 'nexus_synapse_assign_mission':
            return text(await synapse.executeMandatePipeline(String(args.mandateText ?? args.goal ?? ''), {
                budgetUsd: args.budgetUsd == null ? undefined : Number(args.budgetUsd),
                maxOperatives: args.maxOperatives == null ? undefined : Number(args.maxOperatives),
            }));
        case 'nexus_synapse_sortie_start':
        case 'nexus_synapse_sortie_end':
            if (!resolveOperativeId(args.operativeId))
                return text({ error: 'SYNAPSE_OPERATIVE_ID not set' });
            return text(await synapse.runSortie(resolveOperativeId(args.operativeId)));
        case 'nexus_synapse_heartbeat':
            return text(await synapse.triggerHeartbeat(String(args.operativeId), booleanValue(args.force)));
        case 'nexus_synapse_request_approval':
            if (!resolveOperativeId(args.operativeId))
                return text({ error: 'SYNAPSE_OPERATIVE_ID not set' });
            return text(synapse.requestApproval({
                operativeId: resolveOperativeId(args.operativeId),
                missionId: args.missionId ? String(args.missionId) : null,
                scope: args.scope ? String(args.scope) : undefined,
                scopeId: args.scopeId ? String(args.scopeId) : null,
                action: String(args.action),
                description: String(args.description),
                payload: args.payload && typeof args.payload === 'object' ? args.payload : null,
            }));
        case 'nexus_synapse_approval_resolve':
            return text(synapse.resolveApproval(String(args.approvalId), String(args.decision)));
        case 'nexus_synapse_echo':
            if (!resolveOperativeId(args.currentOperativeId))
                return text({ error: 'SYNAPSE_OPERATIVE_ID not set' });
            return text(await synapse.queryEcho({
                missionId: args.missionId ? String(args.missionId) : null,
                missionTitle: String(args.missionTitle),
                currentOperativeId: resolveOperativeId(args.currentOperativeId),
            }));
        case 'nexus_synapse_mission_progress':
            if (!resolveOperativeId(args.operativeId))
                return text({ error: 'SYNAPSE_OPERATIVE_ID not set' });
            return text({
                note: synapse.noteMissionProgress(resolveOperativeId(args.operativeId), args.missionId ? String(args.missionId) : null, String(args.note)),
            });
        case 'nexus_synapse_striketeam_status':
            return text(synapse.getStrikeTeamStatus(args.strikeTeamId ? String(args.strikeTeamId) : undefined));
        case 'nexus_synapse_health':
        case 'nexus_synapse_cost_report':
            return text(synapse.getOperativeHealth(resolveOperativeId(args.operativeId) ?? undefined));
        case 'nexus_synapse_operatives':
            return text(synapse.getOperativeHealth(args.operativeId ? String(args.operativeId) : undefined));
        case 'nexus_synapse_operatives_update':
            return text(synapse.updateOperative({
                operativeId: String(args.operativeId),
                action: args.action ? String(args.action) : undefined,
                roleTitle: args.roleTitle ? String(args.roleTitle) : undefined,
                reportsToOperativeId: args.reportsToOperativeId ? String(args.reportsToOperativeId) : undefined,
                skillId: args.skillId ? String(args.skillId) : undefined,
                specialistId: args.specialistId ? String(args.specialistId) : undefined,
                budgetCapUsd: args.budgetCapUsd == null ? undefined : Number(args.budgetCapUsd),
                sortieIntervalMs: args.sortieIntervalMs == null ? undefined : Number(args.sortieIntervalMs),
                missionId: args.missionId ? String(args.missionId) : undefined,
                strikeTeamId: args.strikeTeamId ? String(args.strikeTeamId) : undefined,
                reason: args.reason ? String(args.reason) : undefined,
            }));
        case 'nexus_synapse_field_report':
            return text(synapse.submitFieldReport(args.report));
        case 'nexus_synapse_hire':
            return text(synapse.hireOperative({
                name: args.name ? String(args.name) : undefined,
                roleTitle: args.roleTitle ? String(args.roleTitle) : undefined,
                reportsToOperativeId: args.reportsToOperativeId ? String(args.reportsToOperativeId) : undefined,
                skillId: args.skillId ? String(args.skillId) : undefined,
                specialistId: args.specialistId ? String(args.specialistId) : undefined,
                budgetCapUsd: args.budgetCapUsd == null ? undefined : Number(args.budgetCapUsd),
                sortieIntervalMs: args.sortieIntervalMs == null ? undefined : Number(args.sortieIntervalMs),
                strikeTeamId: args.strikeTeamId ? String(args.strikeTeamId) : undefined,
                requestApproval: booleanValue(args.requestApproval),
                requestedByOperativeId: args.requestedByOperativeId ? String(args.requestedByOperativeId) : undefined,
            }));
        default:
            return null;
    }
}
