#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

const rootDir = path.join(__dirname, '..');
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
const zipName = `game-portal-${timestamp}.zip`;

console.log('📦 Creating deployment package...\n');

// Files and directories to include
const includes = [
    'games',
    'public',
    'server',
    'scripts',
    'admin',
    '.env',
    'package.json',
    'package-lock.json'
];

// Check if zip command is available
try {
    execSync('which zip', { stdio: 'ignore' });
} catch (error) {
    console.error('❌ Error: zip command not found. Please install zip utility.');
    process.exit(1);
}

// Create zip file
console.log(`Creating ${zipName}...`);

try {
    // Build the zip command
    const zipCommand = `zip -r ${zipName} ${includes.join(' ')} -x "*/node_modules/*" "*/.DS_Store" "*.git*"`;
    
    execSync(zipCommand, { 
        cwd: rootDir,
        stdio: 'inherit'
    });
    
    // Get file size
    const stats = fs.statSync(path.join(rootDir, zipName));
    const fileSizeMB = (stats.size / (1024 * 1024)).toFixed(2);
    
    console.log('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
    console.log(`✅ Deployment package created successfully!`);
    console.log(`📁 File: ${zipName}`);
    console.log(`📊 Size: ${fileSizeMB} MB`);
    console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
    console.log('\n📤 Upload this file to cPanel and extract it in your application directory.');
    
} catch (error) {
    console.error('\n❌ Error creating zip file:', error.message);
    process.exit(1);
}
