import { consumeRestartIntent } from '../services/restart-intent-store.js';
import { countActiveSessionsOnDisk } from '../services/session-store.js';
import { botmuxVersion } from '../utils/install-info.js';
import { t, localeForBot } from '../i18n/index.js';
export const GITHUB_REPO = 'deepcoldy/botmux';
function vtag(v) {
    return v.startsWith('v') ? v : `v${v}`;
}
/** The human-facing markdown body of the report. Pure — unit tested. */
export function buildRestartReportText(input, locale) {
    const lines = [];
    lines.push(input.kind === 'update'
        ? t('restart.updated_restarted', undefined, locale)
        : t('restart.restarted', undefined, locale));
    if (input.kind === 'update' && input.oldVersion && input.newVersion) {
        lines.push(t('restart.version_delta', { old: vtag(input.oldVersion), new: vtag(input.newVersion) }, locale));
    }
    else {
        lines.push(t('restart.version', { version: vtag(input.version) }, locale));
    }
    lines.push(t('restart.unfinished_sessions', { count: input.sessionCount }, locale));
    if (input.dashboardUrl)
        lines.push(t('restart.dashboard', { url: input.dashboardUrl }, locale));
    if (input.kind === 'update' && input.changelog && input.changelog.trim()) {
        lines.push('');
        lines.push(t('restart.changelog_label', undefined, locale));
        lines.push(input.changelog.trim());
    }
    return lines.join('\n');
}
/** Wrap the report body in a minimal Lark interactive card (JSON string). */
export function buildRestartReportCard(input, locale) {
    return JSON.stringify({
        config: { wide_screen_mode: true },
        header: {
            template: input.kind === 'update' ? 'green' : 'blue',
            title: { tag: 'plain_text', content: t('restart.card_title', undefined, locale) },
        },
        elements: [{ tag: 'markdown', content: buildRestartReportText(input, locale) }],
    });
}
export function releasesUrl(version) {
    return `https://github.com/${GITHUB_REPO}/releases/tag/${vtag(version)}`;
}
/**
 * If an intentional-restart breadcrumb is pending, DM the owner a restart
 * summary (exactly once — the breadcrumb is consumed). A crash / pm2
 * auto-restart leaves no breadcrumb, so this stays silent. Call only on the
 * primary daemon after sessions are restored.
 */
export async function sendRestartReportIfPending(w) {
    const log = w.log ?? (() => { });
    const intent = consumeRestartIntent(w.now ?? Date.now());
    if (!intent)
        return; // no breadcrumb → crash/reboot → stay silent
    if (!w.ownerOpenId) {
        log('restart-report: no owner configured — skipping DM');
        return;
    }
    const locale = localeForBot(w.primaryLarkAppId);
    const sessionCount = countActiveSessionsOnDisk();
    const version = botmuxVersion();
    let changelog;
    if (intent.kind === 'update' && intent.newVersion) {
        changelog = (await fetchChangelog(intent.newVersion))
            ?? t('restart.changelog_link_fallback', { url: releasesUrl(intent.newVersion) }, locale);
    }
    const card = buildRestartReportCard({
        kind: intent.kind,
        version,
        sessionCount,
        dashboardUrl: w.dashboardUrl,
        oldVersion: intent.oldVersion,
        newVersion: intent.newVersion,
        changelog,
    }, locale);
    try {
        await w.sendCard(w.ownerOpenId, card);
        log(`restart-report sent (kind=${intent.kind}, sessions=${sessionCount})`);
    }
    catch (e) {
        log(`restart-report send failed: ${e instanceof Error ? e.message : e}`);
    }
}
/** Best-effort GitHub release notes for a version. null on any failure (offline,
 *  rate-limited, release not yet published) — caller falls back to a link. */
export async function fetchChangelog(newVersion) {
    try {
        const res = await fetch(`https://api.github.com/repos/${GITHUB_REPO}/releases/tags/${vtag(newVersion)}`, {
            headers: { Accept: 'application/vnd.github+json', 'User-Agent': 'botmux' },
            signal: AbortSignal.timeout(8_000),
        });
        if (!res.ok)
            return null;
        const body = await res.json();
        const notes = (body?.body ?? '').trim();
        return notes || null;
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=restart-report.js.map