/**
 * `agents feedback` — frictionless, in-CLI feedback. Opens a Discussion
 * pre-filled with version + OS + agent inventory; falls back to printing the
 * URL when no browser is available.
 */
import { arch, platform, release } from 'node:os';
import { createRequire } from 'node:module';
import chalk from 'chalk';
import { showUrl } from '../lib/open-url.js';
const REPO = 'phnx-labs/agi-cli';
const DISCUSSION_BASE = `https://github.com/${REPO}/discussions/new`;
const ISSUE_BASE = `https://github.com/${REPO}/issues/new`;
function readCliVersion() {
    try {
        const require = createRequire(import.meta.url);
        const pkg = require('../../package.json');
        return pkg.version ?? 'unknown';
    }
    catch {
        return 'unknown';
    }
}
function buildPrefill(kind, summary) {
    const version = readCliVersion();
    const os = `${platform()} ${release()} (${arch()})`;
    const node = process.version;
    const body = [
        summary.trim() ? `${summary.trim()}\n` : '<!-- describe what you ran into / what you want -->\n',
        '---',
        '',
        '**Environment**',
        '',
        `- agents-cli: \`${version}\``,
        `- OS: \`${os}\``,
        `- Node: \`${node}\``,
        '',
        '<!-- For bugs: include the exact command you ran and the full output. -->',
        '<!-- For ideas: include a concrete use case. -->'
    ].join('\n');
    if (kind === 'bug') {
        const url = `${ISSUE_BASE}?template=bug_report.yml&title=${encodeURIComponent(summary || 'Bug: ')}`;
        return { url, body };
    }
    const category = kind === 'idea' ? 'ideas' : 'q-a';
    const url = `${DISCUSSION_BASE}?category=${category}&title=${encodeURIComponent(summary || '')}&body=${encodeURIComponent(body)}`;
    return { url, body };
}
export function registerFeedbackCommand(program) {
    program
        .command('feedback [summary...]')
        .description('Open a pre-filled feedback Discussion or bug report')
        .option('-b, --bug', 'File as a bug report (opens issue tracker)')
        .option('-i, --idea', 'File as a feature idea (Discussions → Ideas)')
        .option('-q, --question', 'Ask a question (Discussions → Q&A)')
        .option('--print', 'Print the URL instead of opening it')
        .action(async (summary, opts) => {
        const kind = opts.bug ? 'bug' : opts.idea ? 'idea' : 'question';
        const summaryText = (summary ?? []).join(' ').trim();
        const { url } = buildPrefill(kind, summaryText);
        if (opts.print) {
            console.log(url);
            return;
        }
        const opened = (await showUrl(url)).via !== 'none';
        if (opened) {
            console.log(chalk.dim(`Opened ${kind} form in your browser:\n  ${url}`));
        }
        else {
            console.log(chalk.yellow('Could not auto-open a browser. Paste this URL:'));
            console.log(`  ${url}`);
        }
    });
}
