#!/usr/bin/env node
// Copyright (c) 2026 Hellmai Ltd
// SPDX-License-Identifier: AGPL-3.0-only
/**
 * @file lane-list.ts
 * WU-2592: Read-only listing of lane definitions.
 *
 * Onboarding agents previously resorted to grepping workspace.yaml to
 * discover lane names. lane:list prints a readable table (or JSON) of the
 * current lane definitions so subsequent lane:edit/lane:remove calls can
 * target real names.
 */
import path from 'node:path';
import { existsSync } from 'node:fs';
import { findProjectRoot, getWorkspaceInitCommand, WORKSPACE_CONFIG_FILE_NAME, } from '@lumenflow/core/config';
import { die } from '@lumenflow/core/error-handler';
import { loadLaneDefinitions } from './lane-health.js';
import { runCLI } from './cli-entry-point.js';
const LOG_PREFIX = '[lane:list]';
const ARG_HELP = '--help';
const ARG_JSON = '--json';
function getLaneListHelpText() {
    return `Usage: pnpm lane:list [options]

Print lane definitions from workspace.yaml.

Read-only command. Use lane:edit, lane:create, or lane:remove to modify
lane definitions.

Options:
  ${ARG_JSON}    Output as JSON (for scripting)
  ${ARG_HELP}    Show this help text and exit

Examples:
  pnpm lane:list
  pnpm lane:list --json | jq '.[] | .name'
`;
}
export function parseLaneListArgs(argv) {
    return {
        help: argv.includes(ARG_HELP),
        json: argv.includes(ARG_JSON),
    };
}
function ensureLumenflowInit(projectRoot) {
    const configPath = path.join(projectRoot, WORKSPACE_CONFIG_FILE_NAME);
    if (!existsSync(configPath)) {
        die(`${LOG_PREFIX} Missing ${WORKSPACE_CONFIG_FILE_NAME}.\n\n` +
            `Run \`${getWorkspaceInitCommand(projectRoot)}\` first.`);
    }
}
export function toLaneListRows(definitions) {
    return definitions.map((lane) => {
        const paths = lane.code_paths ?? [];
        return {
            name: lane.name,
            wipLimit: typeof lane.wip_limit === 'number' ? lane.wip_limit : null,
            firstCodePath: paths[0] ?? null,
            codePathCount: paths.length,
        };
    });
}
export function formatLaneListTable(rows) {
    if (rows.length === 0) {
        return `${LOG_PREFIX} No lane definitions found. Run \`pnpm lane:setup\` to create them.`;
    }
    const header = { name: 'NAME', wip: 'WIP', paths: 'CODE PATHS' };
    const nameWidth = Math.max(header.name.length, ...rows.map((r) => r.name.length));
    const wipWidth = Math.max(header.wip.length, ...rows.map((r) => (r.wipLimit === null ? 1 : String(r.wipLimit).length)));
    const lines = [];
    lines.push(`${header.name.padEnd(nameWidth)}  ${header.wip.padEnd(wipWidth)}  ${header.paths}`);
    lines.push(`${'-'.repeat(nameWidth)}  ${'-'.repeat(wipWidth)}  ${'-'.repeat(10)}`);
    for (const row of rows) {
        const wipCell = (row.wipLimit === null ? '-' : String(row.wipLimit)).padEnd(wipWidth);
        const pathCell = row.firstCodePath === null
            ? '(none)'
            : row.codePathCount > 1
                ? `${row.firstCodePath} (+${row.codePathCount - 1} more)`
                : row.firstCodePath;
        lines.push(`${row.name.padEnd(nameWidth)}  ${wipCell}  ${pathCell}`);
    }
    return lines.join('\n');
}
async function main() {
    const userArgs = process.argv.slice(2);
    const { help, json } = parseLaneListArgs(userArgs);
    if (help) {
        console.log(getLaneListHelpText());
        return;
    }
    const projectRoot = findProjectRoot();
    ensureLumenflowInit(projectRoot);
    const definitions = loadLaneDefinitions(projectRoot);
    const rows = toLaneListRows(definitions);
    if (json) {
        console.log(JSON.stringify(rows, null, 2));
        return;
    }
    console.log(formatLaneListTable(rows));
}
if (import.meta.main) {
    void runCLI(main);
}
//# sourceMappingURL=lane-list.js.map