"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderManPage = renderManPage;
const branding_1 = require("../core/branding");
const help_1 = require("./help");
const help_topics_1 = require("./help-topics");
const MAN_PAGE_DATE = "2026-05-10";
const COMMAND_GROUPS = [
    {
        heading: "Setup and health",
        paths: [
            ["init"],
            ["auth", "setup"],
            ["auth", "list"],
            ["doctor"],
            ["docs"],
            ["labels", "sync"],
        ],
    },
    {
        heading: "Daily issue work",
        paths: [
            ["issue", "create"],
            ["issue", "list"],
            ["issue", "show"],
            ["run"],
            ["service", "enable"],
            ["service", "status"],
            ["service", "log", "show"],
        ],
    },
    {
        heading: "Publishing and collaboration",
        paths: [
            ["publish"],
            ["issue", "followup"],
            ["issue", "broadcast"],
            ["issue", "unlock"],
        ],
    },
    {
        heading: "Workflow and skill authoring",
        paths: [
            ["workflow", "create"],
            ["workflow", "update"],
            ["workflow", "delete"],
            ["workflow", "lint"],
            ["workflow", "list"],
            ["event-hook", "config"],
            ["event-hook", "add"],
            ["event-hook", "create"],
            ["event-hook", "update"],
            ["event-hook", "list"],
            ["event-hook", "remove"],
            ["event-hook", "test"],
            ["capability", "list"],
            ["capability", "search"],
            ["skill", "list"],
            ["skill", "search"],
            ["skill", "install"],
            ["skill", "create"],
            ["skill", "catalog", "list"],
            ["skill", "catalog", "add"],
            ["skill", "catalog", "import-github"],
            ["skill", "catalog", "remove"],
            ["mcp", "list"],
            ["mcp", "search"],
            ["mcp", "install"],
            ["mcp", "create"],
            ["mcp", "verify"],
            ["mcp", "catalog", "list"],
            ["mcp", "catalog", "add"],
            ["mcp", "catalog", "remove"],
        ],
    },
    {
        heading: "Maintenance and recovery",
        paths: [
            ["cleanup"],
            ["detach"],
            ["auth", "clear"],
            ["state", "patch"],
            ["state", "restore"],
            ["eject"],
            ["service", "disable"],
            ["service", "restart"],
        ],
    },
];
const TOPIC_BY_PATH = new Map(help_topics_1.HELP_TOPICS.map((topic) => [formatCommandPath(topic.path), topic]));
function renderManPage() {
    return [
        `.TH ${branding_1.PACKAGE_NAME.toUpperCase()} 1 "${MAN_PAGE_DATE}" "${branding_1.SERVICE_DISPLAY_NAME}" "User Commands"`,
        ".SH NAME",
        `${branding_1.PACKAGE_NAME} \\- ${branding_1.SERVICE_DISPLAY_NAME} command line interface`,
        ".SH SYNOPSIS",
        `.B ${branding_1.CLI_COMMAND_NAME}`,
        "[command] [options]",
        ".br",
        `.B ${branding_1.CLI_COMMAND_NAME} docs`,
        "[list|show <topic-id|docs-path>|search <query>]",
        ".br",
        `.B ${branding_1.CLI_COMMAND_NAME} help --all`,
        ".SH DESCRIPTION",
        `${branding_1.SERVICE_DISPLAY_NAME} is a provider-neutral issue and pull request control plane for AI workers.`,
        "It connects repository work, tracker issues, worker configuration, workflow templates, and pull request publication.",
        ".PP",
        "Most users start with repository setup, create or inspect managed issues, then choose either a foreground run or the background service.",
        ".SH QUICK START",
        ".nf",
        `${branding_1.CLI_COMMAND_NAME} init`,
        `${branding_1.CLI_COMMAND_NAME} auth setup  # for codex/claude workers`,
        `${branding_1.CLI_COMMAND_NAME} doctor`,
        `${branding_1.CLI_COMMAND_NAME} issue create --prompt "Fix flaky login tests."`,
        `${branding_1.CLI_COMMAND_NAME} run --issue 123`,
        `${branding_1.CLI_COMMAND_NAME} service enable`,
        `${branding_1.CLI_COMMAND_NAME} service status`,
        `${branding_1.CLI_COMMAND_NAME} issue list --days 7 --limit 10`,
        `${branding_1.CLI_COMMAND_NAME} service log show --tail 50`,
        ".fi",
        ".PP",
        "On Linux and WSL2, service enable requires a working systemd user session (systemctl --user). WSL2 support assumes systemd is enabled; otherwise keep using run for foreground cycles.",
        ".SH COMMANDS",
        ...renderCommandGroups(),
        ".SH RUN MODES",
        ".TP",
        `.B ${branding_1.CLI_COMMAND_NAME} run`,
        "Processes runnable workflow issues once in the foreground. Use this for manual control, targeted issue runs, or local debugging.",
        ".TP",
        `.B ${branding_1.CLI_COMMAND_NAME} service enable`,
        "Installs and starts the background polling service. Use this when AlpacaLoop should keep watching for runnable work.",
        ".PP",
        "Choose one run mode at a time. Stop the service with service disable before using run.",
        ".SH SAFETY NOTES",
        ".IP \\(bu 2",
        "publish finalizes local changes by committing them, opening a pull request, and merging it into the base branch unless reviewers are requested. Review the selected paths, message, and reviewers before confirming.",
        ".IP \\(bu 2",
        "detach without --purge only unregisters the repository. detach --purge can delete managed repository files and workspaces after confirmation.",
        ".IP \\(bu 2",
        "cleanup removes machine-local state or eligible workspaces after confirmation in an interactive terminal.",
        ".IP \\(bu 2",
        "auth clear removes machine-local built-in worker sign-ins. Use --for, --invalid, or --all to keep the scope explicit.",
        ".IP \\(bu 2",
        "service disable --force and service restart --force stop or restart without waiting for current service work to finish.",
        ".IP \\(bu 2",
        "state patch and state restore update canonical workflow state comments. Use state patch --dry-run --state open to check open issue migration before publishing workflow state schema or patch changes.",
        ".SH EXAMPLES",
        ".nf",
        `${branding_1.CLI_COMMAND_NAME} init --repo-provider github`,
        `${branding_1.CLI_COMMAND_NAME} issue create --prompt "Investigate queue delay" --save-draft`,
        `${branding_1.CLI_COMMAND_NAME} issue create --apply-draft ~/.alpaca/drafts/.../issue-create-....json`,
        `${branding_1.CLI_COMMAND_NAME} issue show --issue 123 --children 1 --limit 5`,
        `${branding_1.CLI_COMMAND_NAME} issue search --title "memory" --state all --limit 10`,
        `${branding_1.CLI_COMMAND_NAME} issue broadcast --open --message-file message.txt --dry-run`,
        `${branding_1.CLI_COMMAND_NAME} workflow lint`,
        `${branding_1.CLI_COMMAND_NAME} skill search browser --for codex`,
        `${branding_1.CLI_COMMAND_NAME} mcp search github --for lmstudio-coder`,
        `${branding_1.CLI_COMMAND_NAME} service disable`,
        ".fi",
        ".SH FILES",
        ".TP",
        ".I alpaca.config.json",
        "Repository-local AlpacaLoop configuration created or updated by init.",
        ".TP",
        ".I .alpaca/workflows/",
        "Repository-local workflow templates. Ejected packaged workflows live under .alpaca/workflows/__system/.",
        ".TP",
        ".I .alpaca/system-prompts/",
        "Repository-local optional system prompt guidance ejected for editing.",
        ".TP",
        ".I .alpaca/catalog/",
        "Repository-local skill and MCP catalog entries.",
        ".TP",
        ".I skills/alpaca/references/",
        "Packaged AlpacaLoop reference docs used by the docs command.",
        ".TP",
        ".I ~/.alpaca/",
        "Machine-local AlpacaLoop state, including auth profile metadata, drafts, and logs under ~/.alpaca/logs/ unless evidenceRoot routes logs elsewhere.",
        ".SH HELP",
        ".nf",
        `${branding_1.CLI_COMMAND_NAME} --help`,
        `${branding_1.CLI_COMMAND_NAME} help --all`,
        `${branding_1.CLI_COMMAND_NAME} <command> --help`,
        `${branding_1.CLI_COMMAND_NAME} docs`,
        `${branding_1.CLI_COMMAND_NAME} docs list`,
        `${branding_1.CLI_COMMAND_NAME} docs show config`,
        `${branding_1.CLI_COMMAND_NAME} docs search <query>`,
        ".fi",
        ".SH COMMAND REFERENCE",
        "The following reference is generated from the same help topics used by the CLI.",
        ".nf",
        ...(0, help_1.renderAllHelp)("en").split("\n").map(escapeRoffPreformattedLine),
        ".fi",
        ".SH SEE ALSO",
        ".BR git (1),",
        ".BR gh (1)",
        "",
    ].join("\n");
}
function renderCommandGroups() {
    return COMMAND_GROUPS.flatMap((group) => [
        ".SS",
        group.heading,
        ...group.paths.flatMap(renderGroupedCommand),
    ]);
}
function renderGroupedCommand(path) {
    const topic = TOPIC_BY_PATH.get(formatCommandPath(path));
    const summary = topic?.summary ?? "";
    return [".TP", `.B ${branding_1.CLI_COMMAND_NAME} ${formatCommandPath(path)}`, summary];
}
function formatCommandPath(path) {
    return path.join(" ");
}
function escapeRoffPreformattedLine(line) {
    const escaped = line.replace(/\\/g, "\\e");
    return /^[.']/.test(escaped) ? `\\&${escaped}` : escaped;
}
