#!/usr/bin/env node
/**
 * ReePoe Extension Request - User requests access extension
 */

const ActivationClient = require('../lib/activation-client');
const readline = require('readline');

async function requestExtension() {
    const client = new ActivationClient();
    
    // Load activation
    const activation = client.loadActivation();
    
    if (!activation) {
        console.log('\n❌ No activation found');
        console.log('   Please activate ReePoe first: reepoe-start\n');
        process.exit(1);
    }
    
    console.log('\n╔═══════════════════════════════════════════════════════════════╗');
    console.log('║  📧 Request Access Extension                                  ║');
    console.log('╚═══════════════════════════════════════════════════════════════╝\n');
    
    console.log(`Your current access expires:`);
    if (activation.expires_at) {
        const expiresDate = new Date(activation.expires_at);
        const daysRemaining = Math.ceil((expiresDate - new Date()) / (1000 * 60 * 60 * 24));
        console.log(`   ${expiresDate.toLocaleDateString()} (${daysRemaining} days remaining)\n`);
    } else {
        console.log('   Lifetime access\n');
    }
    
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    
    const reason = await new Promise((resolve) => {
        rl.question('💬 Reason for extension (optional): ', (answer) => {
            rl.close();
            resolve(answer.trim());
        });
    });
    
    console.log('\n⏳ Sending extension request...\n');
    
    try {
        const result = await client.requestExtension(activation.email, reason || 'Standard extension request');
        
        if (result.success) {
            console.log('╔═══════════════════════════════════════════════════════════════╗');
            console.log('║  ✅ Extension Request Sent!                                   ║');
            console.log('╚═══════════════════════════════════════════════════════════════╝\n');
            console.log('Your request has been sent to the admin team.');
            console.log('You\'ll be notified when it\'s reviewed.\n');
            console.log('💡 Typically reviewed within 24-48 hours.\n');
        } else {
            console.log(`❌ Request failed: ${result.message}\n`);
        }
        
    } catch (error) {
        console.error(`\n❌ Error: ${error.message}\n`);
        process.exit(1);
    }
}

requestExtension();

