import { buildWorkbenchSummary } from './workbench-summary.js';
export function parseWorkbenchArgs(args) {
    const options = {
        limit: 10,
        json: false,
        help: false,
    };
    for (let index = 0; index < args.length; index += 1) {
        const arg = args[index];
        if (arg === '--') {
            continue;
        }
        if (arg === '--help' || arg === '-h') {
            options.help = true;
            continue;
        }
        if (arg === '--json') {
            options.json = true;
            continue;
        }
        if (arg === '--project') {
            options.project = readOptionValue(args, index, arg);
            index += 1;
            continue;
        }
        if (arg === '--limit') {
            options.limit = readPositiveInteger(readOptionValue(args, index, arg), arg);
            index += 1;
            continue;
        }
        if (arg === '--api-base') {
            options.apiBase = readOptionValue(args, index, arg);
            index += 1;
            continue;
        }
        throw new Error(`Unknown option: ${arg}\n\n${workbenchUsage()}`);
    }
    return options;
}
export async function runWorkbenchSummary(services, options) {
    return buildWorkbenchSummary(services, {
        project: options.project,
        limit: options.limit,
    });
}
export function formatWorkbenchSummary(summary, options = {}) {
    const lines = [
        '# Tuberosa Workbench',
        '',
        `Generated: ${summary.generatedAt}`,
        `Filters: ${formatFilters(summary.filters)}`,
        `Workbench: ${endpoint('/workbench', options.apiBase)}`,
        `Summary API: ${endpointWithQuery('/operations/workbench/summary', {
            project: summary.filters.project,
            limit: summary.filters.limit,
        }, options.apiBase)}`,
        '',
        '## Health',
        `Store: ${summary.health.store} (${summary.health.durability})`,
        `Cache: ${summary.health.cache}`,
        `Model provider: ${summary.health.modelProvider}`,
        `Backups: ${summary.health.backupStatus.health}; count=${summary.health.backupStatus.backupCount}; dir=${summary.health.backupDir}`,
        '',
        '## Counts',
        `Count scan limit: ${summary.countMetadata.scanLimit}; capped values end with +`,
        `Recent sessions: ${formatCount(summary, 'recentSessions')}; active=${formatCount(summary, 'activeSessions')}`,
        `Context quality: matched=${formatCount(summary, 'contextQualityMatched')}; showing=${formatCount(summary, 'contextQualityRecords')}`,
        `Pending drafts: ${formatCount(summary, 'pendingDrafts')}`,
        `Open gaps/proposals/conflicts: ${formatCount(summary, 'openGaps')}/${formatCount(summary, 'openProposals')}/${formatCount(summary, 'openConflicts')}`,
        `Auto memories: ${formatCount(summary, 'autoMemories')}; risky=${formatCount(summary, 'riskyAutoMemories')}`,
        `Open error logs: ${formatCount(summary, 'openErrorLogs')}`,
        `Pending maintenance: ${formatCount(summary, 'pendingMaintenance')}`,
        '',
        '## Recommended Actions',
        ...formatRecommendedActions(summary.recommendedActions, options.apiBase),
        '',
        '## Recent Sessions',
        ...formatRecentSessions(summary),
        '',
        '## Context Quality',
        ...formatContextQuality(summary, options.apiBase),
        '',
        '## Memory Review',
        ...formatMemoryReview(summary, options.apiBase),
        '',
        '## Error Logs',
        ...formatErrorLogs(summary.openErrorLogs.logs, options.apiBase),
    ];
    return lines.join('\n');
}
export function workbenchUsage() {
    return [
        'Usage: pnpm run workbench -- [--project <name>] [--limit <n>] [--api-base <url>] [--json]',
        '',
        'Prints a read-only local workbench summary from existing Tuberosa operations APIs.',
    ].join('\n');
}
function formatRecommendedActions(actions, apiBase) {
    return actions.map((action, index) => {
        const href = action.href ? ` | ${endpoint(action.href, apiBase)}` : '';
        return `${index + 1}. ${action.label} (${action.count})${href}\n   Reason: ${action.reason}`;
    });
}
function formatRecentSessions(summary) {
    if (summary.recentSessions.length === 0) {
        return ['No recent sessions matched these filters.'];
    }
    return summary.recentSessions.map((session) => {
        const outcome = session.outcome ? `/${session.outcome}` : '';
        return `- ${session.id} | ${session.status}${outcome} | ${truncateLine(session.prompt, 100)}`;
    });
}
function formatContextQuality(summary, apiBase) {
    if (summary.contextQuality.records.length === 0) {
        return ['No context-quality feedback matched these filters.'];
    }
    return summary.contextQuality.records.map((record) => {
        const pack = record.contextPack
            ? ` | pack=${endpoint(`/context/packs/${encodeURIComponent(record.contextPack.id)}`, apiBase)}`
            : '';
        return `- ${record.feedback.feedbackType} (${record.feedback.id})${pack} | actions=${record.suggestedReviewActions.length}`;
    });
}
function formatMemoryReview(summary, apiBase) {
    const lines = [];
    appendDrafts(lines, summary.pendingDrafts, apiBase);
    appendAutoMemories(lines, summary.riskyAutoMemories, apiBase);
    appendGaps(lines, summary.openGaps, apiBase);
    appendProposals(lines, summary.openProposals, apiBase);
    appendConflicts(lines, summary.openConflicts, apiBase);
    return lines.length > 0 ? lines : ['No pending memory review items matched these filters.'];
}
function appendDrafts(lines, drafts, apiBase) {
    if (drafts.length === 0) {
        return;
    }
    lines.push('Pending drafts:');
    for (const draft of drafts) {
        lines.push(`- ${draft.title} (${draft.id}) | ${endpoint(`/reflection-drafts/${encodeURIComponent(draft.id)}`, apiBase)}`);
    }
}
function appendAutoMemories(lines, memories, apiBase) {
    if (memories.length === 0) {
        return;
    }
    lines.push('Risky auto memories:');
    for (const memory of memories) {
        lines.push(`- ${memory.title} (${memory.id}) | status=${memory.status ?? 'approved'} | ${endpoint(`/knowledge/${encodeURIComponent(memory.id)}`, apiBase)}`);
    }
}
function appendGaps(lines, gaps, apiBase) {
    if (gaps.length === 0) {
        return;
    }
    lines.push('Open gaps:');
    for (const gap of gaps) {
        lines.push(`- ${gap.id} | missing=${formatStringList(gap.missingSignals, 5)} | ${endpoint(`/operations/knowledge-gaps/${encodeURIComponent(gap.id)}`, apiBase)}`);
    }
}
function appendProposals(lines, proposals, apiBase) {
    if (proposals.length === 0) {
        return;
    }
    lines.push('Open proposals:');
    for (const proposal of proposals) {
        lines.push(`- ${proposal.proposalType} (${proposal.id}) | ${truncateLine(proposal.reason, 100)} | ${endpoint(`/operations/learning-proposals/${encodeURIComponent(proposal.id)}`, apiBase)}`);
    }
}
function appendConflicts(lines, conflicts, apiBase) {
    if (conflicts.length === 0) {
        return;
    }
    lines.push('Open conflicts:');
    for (const conflict of conflicts) {
        lines.push(`- ${conflict.conflictType} (${conflict.id}) | ${endpoint(`/operations/conflicts/${encodeURIComponent(conflict.id)}`, apiBase)}`);
    }
}
function formatErrorLogs(logs, apiBase) {
    if (logs.length === 0) {
        return ['No open or triaged error logs matched these filters.'];
    }
    return logs.map((log) => (`- ${log.title} (${log.id}) | ${log.status}/${log.severity} | ${endpoint(`/operations/error-logs/${encodeURIComponent(log.id)}`, apiBase)}`));
}
function formatFilters(filters) {
    return [
        filters.project ? `project=${filters.project}` : 'project=all',
        `limit=${filters.limit}`,
    ].join('; ');
}
function formatCount(summary, key) {
    const value = summary.counts[key];
    return summary.countMetadata.capped[key] ? `${value}+` : String(value);
}
function endpointWithQuery(path, params, apiBase) {
    const query = new URLSearchParams();
    for (const [key, value] of Object.entries(params)) {
        if (value !== undefined && value !== '') {
            query.set(key, String(value));
        }
    }
    const serialized = query.toString();
    return endpoint(serialized ? `${path}?${serialized}` : path, apiBase);
}
function endpoint(path, apiBase) {
    const normalizedPath = path.startsWith('/') ? path : `/${path}`;
    if (!apiBase) {
        return normalizedPath;
    }
    return `${apiBase.replace(/\/+$/, '')}${normalizedPath}`;
}
function formatStringList(values, max) {
    if (values.length === 0) {
        return 'none';
    }
    const visible = values.slice(0, max);
    const suffix = values.length > max ? `, +${values.length - max} more` : '';
    return `${visible.join(', ')}${suffix}`;
}
function truncateLine(value, max) {
    return value.length > max ? `${value.slice(0, max - 3)}...` : value;
}
function readOptionValue(args, index, option) {
    const value = args[index + 1];
    if (!value || value.startsWith('--')) {
        throw new Error(`${option} requires a value.`);
    }
    return value;
}
function readPositiveInteger(value, option) {
    if (!/^\d+$/.test(value)) {
        throw new Error(`${option} requires a positive integer.`);
    }
    const parsed = Number.parseInt(value, 10);
    if (parsed < 1) {
        throw new Error(`${option} requires a positive integer.`);
    }
    return Math.min(parsed, 100);
}
