"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.activateCommand = activateCommand;
exports.deactivateCommand = deactivateCommand;
exports.statusCommand = statusCommand;
const logger_js_1 = require("../utils/logger.js");
const prompt_js_1 = require("../utils/prompt.js");
const license_js_1 = require("../utils/license.js");
async function activateCommand() {
    logger_js_1.logger.info('🔑 Activate Craft License\n');
    const key = await (0, prompt_js_1.promptInput)('Enter your license key:', 'CRAFT-PRO-XXXX-XXXX-XXXX-XXXX-XXXX');
    if (!(0, license_js_1.validateLicenseKeyFormat)(key)) {
        logger_js_1.logger.error('❌ Invalid license key format');
        logger_js_1.logger.info('Format: CRAFT-{TIER}-XXXX-XXXX-XXXX-XXXX-XXXX');
        logger_js_1.logger.info('Example: CRAFT-PRO-A1B2-C3D4-E5F6-G7H8-1A2B\n');
        return;
    }
    logger_js_1.logger.step('Validating license...');
    try {
        const deviceId = (0, license_js_1.generateDeviceId)();
        const license = await (0, license_js_1.validateLicenseOnline)(key, deviceId);
        (0, license_js_1.saveLicense)(license);
        logger_js_1.logger.success('✅ License activated successfully!\n');
        logger_js_1.logger.info(`Tier: ${getTierName(license.tier)}`);
        logger_js_1.logger.info(`Email: ${license.email}`);
        logger_js_1.logger.info(`Device ID: ${deviceId}\n`);
        const levelAccess = (0, license_js_1.getLevelAccess)(license.tier);
        const platformAccess = (0, license_js_1.getPlatformAccess)(license.tier);
        logger_js_1.logger.info('Access:');
        logger_js_1.logger.info(`  • Rapid level: ${levelAccess.rapid ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • Balanced level: ${levelAccess.balanced ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • Crafted level: ${levelAccess.crafted ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • Web platform: ${platformAccess.web ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • Mobile platform: ${platformAccess.mobile ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • AI Skills: ${(0, license_js_1.getSkillCount)(license.tier)}`);
        logger_js_1.logger.info(`  • MCP Servers: ${(0, license_js_1.getMcpServerCount)(license.tier)}\n`);
    }
    catch (error) {
        logger_js_1.logger.error(`❌ Activation failed: ${error.message}`);
        logger_js_1.logger.info('\nPossible reasons:');
        logger_js_1.logger.info('  • Invalid license key');
        logger_js_1.logger.info('  • License already activated on max devices (3)');
        logger_js_1.logger.info('  • License expired or revoked');
        logger_js_1.logger.info('  • Network connection issue\n');
        logger_js_1.logger.info('Get help: https://craft.dev/support\n');
    }
}
async function deactivateCommand() {
    logger_js_1.logger.info('🔓 Deactivate Craft License\n');
    const license = (0, license_js_1.loadLicense)();
    if (!license) {
        logger_js_1.logger.warn('No license found');
        return;
    }
    logger_js_1.logger.step('Deactivating license...');
    try {
        const deviceId = (0, license_js_1.generateDeviceId)();
        const API_URL = process.env.CRAFT_API_URL || 'https://getcraft.dev';
        const response = await fetch(`${API_URL}/api/v1/licenses/deactivate`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ key: license.key, deviceId }),
        });
        if (!response.ok) {
            throw new Error('Deactivation failed');
        }
        const fs = await import('fs');
        const licenseFile = await import('../utils/license.js').then(m => m.getLicenseFile());
        fs.unlinkSync(licenseFile);
        logger_js_1.logger.success('✅ License deactivated successfully!\n');
        logger_js_1.logger.info('You can activate it on another device.\n');
    }
    catch (error) {
        logger_js_1.logger.error(`❌ Deactivation failed: ${error.message}`);
        logger_js_1.logger.info('Contact support if you need help: https://craft.dev/support\n');
    }
}
async function statusCommand() {
    logger_js_1.logger.info('📊 Craft License Status\n');
    try {
        const license = await (0, license_js_1.checkLicense)();
        if (!license.key) {
            logger_js_1.logger.warn('No license activated (Community tier)\n');
            logger_js_1.logger.info('Community tier includes:');
            logger_js_1.logger.info('  • Rapid level only');
            logger_js_1.logger.info('  • 5 AI Skills (rapid, feature, bugfix, dev, test)');
            logger_js_1.logger.info('  • Web only (no mobile)');
            logger_js_1.logger.info('  • 4 basic MCP servers\n');
            logger_js_1.logger.info('Upgrade to Pro for full access:');
            logger_js_1.logger.info('  → https://craft.dev/pricing\n');
            return;
        }
        if (!license.valid) {
            logger_js_1.logger.error('❌ License validation failed\n');
            logger_js_1.logger.info('Your license could not be validated:');
            logger_js_1.logger.info('  • Offline validation expired (7 days grace period)');
            logger_js_1.logger.info('  • Please connect to the internet and run: craft license status\n');
            return;
        }
        logger_js_1.logger.success(`✅ ${getTierName(license.tier)} License Active\n`);
        logger_js_1.logger.info(`Email: ${license.email}`);
        logger_js_1.logger.info(`Device ID: ${license.deviceId}`);
        logger_js_1.logger.info(`Last validated: ${license.lastValidated ? new Date(license.lastValidated).toLocaleString() : 'Never'}`);
        logger_js_1.logger.info(`Status: ${license.offline ? '⚠️ Offline (grace period)' : '✅ Online'}\n`);
        const levelAccess = (0, license_js_1.getLevelAccess)(license.tier);
        const platformAccess = (0, license_js_1.getPlatformAccess)(license.tier);
        logger_js_1.logger.info('Access:');
        logger_js_1.logger.info(`  • Rapid level: ${levelAccess.rapid ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • Balanced level: ${levelAccess.balanced ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • Crafted level: ${levelAccess.crafted ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • Web platform: ${platformAccess.web ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • Mobile platform: ${platformAccess.mobile ? '✅' : '❌'}`);
        logger_js_1.logger.info(`  • AI Skills: ${(0, license_js_1.getSkillCount)(license.tier)}`);
        logger_js_1.logger.info(`  • MCP Servers: ${(0, license_js_1.getMcpServerCount)(license.tier)}\n`);
        if (license.tier === 'COMM') {
            logger_js_1.logger.info('💡 Upgrade to unlock:');
            logger_js_1.logger.info('  • Balanced + Crafted levels (100-10K+ users)');
            logger_js_1.logger.info('  • 12 additional AI Skills (17 total)');
            logger_js_1.logger.info('  • Web + Mobile (React Native + Expo)');
            logger_js_1.logger.info('  • 6 additional MCP servers (10 total)');
            logger_js_1.logger.info('  • Premium templates\n');
            logger_js_1.logger.info('Upgrade: https://craft.dev/pricing\n');
        }
    }
    catch (error) {
        logger_js_1.logger.error(`❌ Error: ${error.message}\n`);
    }
}
function getTierName(tier) {
    switch (tier) {
        case 'COMM':
            return 'Community';
        case 'PRO':
            return 'Pro';
        case 'ENT':
            return 'Enterprise';
        default:
            return 'Unknown';
    }
}
//# sourceMappingURL=license.js.map