import { existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

console.log('🔍 Verifying Email Sortierer Setup\n');

let allChecksPass = true;

// Check 1: .env file exists
console.log('Check 1: .env file exists');
const envPath = join(__dirname, '.env');
if (existsSync(envPath)) {
    console.log('✅ .env file found\n');
} else {
    console.log('❌ .env file not found');
    console.log('   → Copy .env.example to .env and fill in your credentials\n');
    allChecksPass = false;
}

// Check 2: Required environment variables
console.log('Check 2: Required environment variables');
const requiredVars = [
    'APPWRITE_ENDPOINT',
    'APPWRITE_PROJECT_ID',
    'APPWRITE_API_KEY',
    'APPWRITE_DATABASE_ID',
    'STRIPE_SECRET_KEY',
    'STRIPE_WEBHOOK_SECRET'
];

let missingVars = [];
for (const varName of requiredVars) {
    if (!process.env[varName]) {
        missingVars.push(varName);
    }
}

if (missingVars.length === 0) {
    console.log('✅ All required environment variables are set\n');
} else {
    console.log('❌ Missing environment variables:');
    missingVars.forEach(v => console.log(`   - ${v}`));
    console.log('   → Add these to your .env file\n');
    allChecksPass = false;
}

// Check 3: Node modules installed
console.log('Check 3: Node modules installed');
const nodeModulesPath = join(__dirname, 'node_modules');
if (existsSync(nodeModulesPath)) {
    console.log('✅ node_modules found\n');
} else {
    console.log('❌ node_modules not found');
    console.log('   → Run: npm install\n');
    allChecksPass = false;
}

// Check 4: Required files exist
console.log('Check 4: Required files exist');
const requiredFiles = [
    'index.mjs',
    'bootstrap-appwrite.mjs',
    'e2e-test.mjs',
    'package.json',
    '../public/index.html'
];

let missingFiles = [];
for (const file of requiredFiles) {
    const filePath = join(__dirname, file);
    if (!existsSync(filePath)) {
        missingFiles.push(file);
    }
}

if (missingFiles.length === 0) {
    console.log('✅ All required files exist\n');
} else {
    console.log('❌ Missing files:');
    missingFiles.forEach(f => console.log(`   - ${f}`));
    console.log('\n');
    allChecksPass = false;
}

// Summary
console.log('═══════════════════════════════════════════════════════');
if (allChecksPass) {
    console.log('✅ Setup verification passed!');
    console.log('\nNext steps:');
    console.log('1. Run bootstrap script: npm run bootstrap');
    console.log('2. Update APPWRITE_DATABASE_ID in .env with the database ID from bootstrap');
    console.log('3. Start the server: npm start');
    console.log('4. Run tests: npm test');
    console.log('5. Open browser: http://localhost:3000');
} else {
    console.log('❌ Setup verification failed');
    console.log('\nPlease fix the issues above and run this script again.');
    console.log('Run: node verify-setup.mjs');
}
console.log('═══════════════════════════════════════════════════════');
