// tools/license-status.ts — Handler for license_status MCP tool.
// Shows current tier, limits, usage, and upgrade URL.
import { licenseStore } from '../storage/index.js';
import { getTierLimits, loadLicensePlans } from '../engine/license-validator.js';
import { getCurrentTier } from './license-gate.js';
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
function getVersion() {
    try {
        const dir = dirname(fileURLToPath(import.meta.url));
        const pkg = JSON.parse(readFileSync(join(dir, '../../package.json'), 'utf-8'));
        return pkg.version;
    }
    catch {
        return 'unknown';
    }
}
export async function handleLicenseStatus(_args) {
    const [state, tier] = await Promise.all([licenseStore.getLicenseState(), getCurrentTier()]);
    const limits = getTierLimits(tier);
    const config = loadLicensePlans();
    const projectCount = await licenseStore.countActiveProjects();
    const lines = [];
    lines.push(`## Planu License Status`);
    lines.push('');
    const tierLabel = tier.charAt(0).toUpperCase() + tier.slice(1);
    const totalTools = config.freeTools.length + config.proTools.length + config.alwaysAllowed.length;
    lines.push(`**Version:** ${getVersion()}`);
    lines.push(`**Plan:** ${tierLabel}`);
    if (state?.customerName) {
        lines.push(`**Customer:** ${state.customerName} (${state.customerEmail})`);
        lines.push(`**Variant:** ${state.variantName}`);
        lines.push(`**Activated:** ${state.activatedAt}`);
        lines.push(`**Last validated:** ${state.lastValidatedAt}`);
    }
    if (tier === 'free') {
        const freeAvailable = config.freeTools.length + config.alwaysAllowed.length;
        lines.push(`**Tools available:** ${String(freeAvailable)} of ${String(totalTools)}`);
    }
    else {
        lines.push(`**Tools available:** ${String(totalTools)} of ${String(totalTools)}`);
    }
    lines.push('');
    lines.push('### Limits');
    lines.push(`- Projects: ${String(projectCount)} / ${limits.maxProjects >= 999999 ? '∞' : String(limits.maxProjects)}`);
    lines.push(`- Active specs: ${limits.maxActiveSpecs >= 999999 ? '∞' : String(limits.maxActiveSpecs)} per project`);
    if (state?.customerName && state.activationLimit > 0) {
        lines.push(`- Activation slots: ${String(state.activationUsage)} / ${String(state.activationLimit)}`);
    }
    if (tier === 'free') {
        lines.push('');
        lines.push('### Upgrade');
        lines.push(`Unlock all ${String(totalTools)} tools, 10 projects, and unlimited specs.`);
        lines.push(`→ ${config.upgradeUrl}`);
        lines.push('');
        lines.push('Already have a key? Use `activate_license` to activate it.');
    }
    const structured = {
        version: getVersion(),
        plan: tierLabel,
        toolsAvailable: tier === 'free' ? config.freeTools.length + config.alwaysAllowed.length : totalTools,
        toolsTotal: totalTools,
        limits: {
            maxProjects: limits.maxProjects,
            currentProjects: projectCount,
            maxActiveSpecs: limits.maxActiveSpecs,
        },
    };
    if (state?.customerName) {
        structured.customer = {
            name: state.customerName,
            email: state.customerEmail,
            variant: state.variantName,
            activatedAt: state.activatedAt,
            lastValidatedAt: state.lastValidatedAt,
        };
        if (state.customerName && state.activationLimit > 0) {
            structured.activationSlots = {
                used: state.activationUsage,
                total: state.activationLimit,
            };
        }
    }
    if (tier === 'free') {
        structured.upgradeUrl = config.upgradeUrl;
    }
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
        structuredContent: structured,
    };
}
//# sourceMappingURL=license-status.js.map