#!/usr/bin/env node

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

console.log('🔍 Checking development environment...\n');

// Check if .env file exists
const envPath = path.join(process.cwd(), '.env');
if (!fs.existsSync(envPath)) {
  console.error('❌ .env file not found');
  console.log('💡 Please create a .env file with the following variables:');
  console.log('   DATABASE_URL="your-database-url"');
  console.log('   NEXT_PUBLIC_SUPABASE_URL="your-supabase-url"');
  console.log('   NEXT_PUBLIC_SUPABASE_ANON_KEY="your-supabase-anon-key"');
  process.exit(1);
}

// Check if Prisma client is generated
const prismaClientPath = path.join(process.cwd(), 'node_modules', '.prisma', 'client');
if (!fs.existsSync(prismaClientPath)) {
  console.log('📦 Prisma client not found, generating...');
  try {
    execSync('npx prisma generate', { stdio: 'inherit' });
    console.log('✅ Prisma client generated successfully\n');
  } catch (error) {
    console.error('❌ Failed to generate Prisma client');
    process.exit(1);
  }
} else {
  console.log('✅ Prisma client found\n');
}

// Check database connection
console.log('🗄️  Checking database connection...');
try {
  execSync('npx prisma db push --accept-data-loss', { stdio: 'pipe' });
  console.log('✅ Database connection successful\n');
} catch (error) {
  console.error('❌ Database connection failed');
  console.log('💡 Please check your DATABASE_URL in .env file');
  console.log('💡 Make sure your database is running and accessible');
  process.exit(1);
}

console.log('🎉 Development environment is ready!');
console.log('🚀 You can now run: npm run dev');
