// ─────────────────────────────────────────────────────────────────────────────
// Nexus Prime Licensing — Upgrade Prompt Templates
//
// Branded messages shown when users approach or exceed tier limits.
// Three levels: nudge (80%), firm (100%), gate (tool not in tier).
// ─────────────────────────────────────────────────────────────────────────────
import { tierLabel } from './tool-tiers.js';
async function emitUpgradeNudge(currentTier, feature, message, ctaUrl) {
    try {
        const { nexusEventBus } = await import('../engines/event-bus.js');
        nexusEventBus.emit('license.upgradeNudge', { currentTier, feature, message, ctaUrl });
    }
    catch { /* non-fatal */ }
}
const UPGRADE_URL = 'https://nexus-prime.cfd/account';
const SIGNUP_URL = 'https://nexus-prime.cfd/signup';
const LICENSE_TEAM_EMAIL = 'adarsh@nexus-prime.cfd';
const CAP_LABELS = {
    memory_entries: 'memory entries',
    projects: 'projects',
    operatives: 'operatives',
};
/**
 * Soft nudge at 80% of cap — single line appended to tool response.
 */
export function capWarningMessage(cap, capType) {
    const label = CAP_LABELS[capType] ?? capType;
    const msg = `${cap.current.toLocaleString()}/${cap.limit.toLocaleString()} ${label} used on ${tierLabel(cap.tier)} plan.`;
    void emitUpgradeNudge(cap.tier, capType, msg, UPGRADE_URL);
    return `[Nexus Prime] ${msg} Upgrade: ${UPGRADE_URL}`;
}
/**
 * Firm prompt at 100% cap — multi-line with upgrade path.
 */
export function capExceededMessage(cap, capType) {
    const label = CAP_LABELS[capType] ?? capType;
    return [
        `[Nexus Prime] ${tierLabel(cap.tier)} plan limit reached: ${cap.current.toLocaleString()}/${cap.limit.toLocaleString()} ${label}.`,
        `  Upgrade your plan to unlock more capacity.`,
        `  → ${UPGRADE_URL}`,
        `  → CLI: nexus-prime license sync`,
    ].join('\n');
}
/**
 * Tool gate — tool requires a higher tier.
 */
export function toolGateMessage(toolName, requiredTier, currentTier) {
    return [
        `[Nexus Prime] ${toolName} requires the ${tierLabel(requiredTier)} plan (current: ${tierLabel(currentTier)}).`,
        `  Upgrade: ${UPGRADE_URL}`,
        `  → CLI: nexus-prime license sync`,
    ].join('\n');
}
/**
 * No license activated — prompt to sign up.
 */
export function noLicenseMessage() {
    return [
        `[Nexus Prime] No license activated. Request a license before paid-tier work runs:`,
        `  → ${SIGNUP_URL}`,
        `  Account: ${UPGRADE_URL}`,
        `  Email: ${LICENSE_TEAM_EMAIL}`,
        `  Licenses are reviewed and shared by the team within 24 hours.`,
        `  Then activate: nexus-prime license activate <your-token>`,
    ].join('\n');
}
/**
 * Trial is active. Soft hint that surfaces alongside paid tools so users
 * know the auto-issued window is what's keeping their runtime alive.
 */
export function trialActiveMessage(statusOrExpiresAt) {
    const status = typeof statusOrExpiresAt === 'object' ? statusOrExpiresAt : null;
    const expiresAt = status ? status.expiresAt : typeof statusOrExpiresAt === 'number' ? statusOrExpiresAt : null;
    const daysLeft = status?.trialDaysRemaining
        ?? (expiresAt ? Math.max(0, Math.ceil((expiresAt - Date.now()) / (24 * 60 * 60 * 1000))) : 0);
    if (status?.activationRequired) {
        return [
            `[Nexus Prime] License activation requested.`,
            `  Agent motion: ask the user to request or activate a Nexus Prime license now, but do not stop free/trial-safe work unless the trial has expired.`,
            `  Trial window: ${daysLeft} day${daysLeft === 1 ? '' : 's'} remaining.`,
            `  Request: ${UPGRADE_URL}`,
            `  Email: ${LICENSE_TEAM_EMAIL}`,
            `  The Nexus Prime team will share licenses within 24 hours.`,
            `  Activate: nexus-prime license activate <token>`,
        ].join('\n');
    }
    if (status?.trialPhase === 'activation') {
        return [
            `[Nexus Prime] Trial active (${daysLeft} day${daysLeft === 1 ? '' : 's'} remaining).`,
            `  Agent motion: the first 3 no-license days are over; ask the user to request or activate a license soon, then keep working.`,
            `  Request: ${UPGRADE_URL}`,
            `  Email: ${LICENSE_TEAM_EMAIL}`,
            `  Licenses are reviewed and shared within 24 hours.`,
        ].join('\n');
    }
    return [
        `[Nexus Prime] 3-day no-license grace active (${daysLeft} day${daysLeft === 1 ? '' : 's'} in the trial window).`,
        `  No license is mandatory during the first 3 days. Request one soon: ${UPGRADE_URL}`,
        `  Email fallback: ${LICENSE_TEAM_EMAIL}`,
        `  Licenses are reviewed and shared within 24 hours.`,
    ].join('\n');
}
/**
 * Trial has expired — runtime drops to free caps. Tell the user clearly so
 * the gap between trial-expired and a real license is obvious.
 */
export function trialExpiredMessage() {
    return [
        `[Nexus Prime] Trial expired — license required for paid-tier work.`,
        `  Request: ${UPGRADE_URL}`,
        `  Email: ${LICENSE_TEAM_EMAIL}`,
        `  Licenses are reviewed and shared within 24 hours.`,
        `  Activate: nexus-prime license activate <token>`,
    ].join('\n');
}
