#!/usr/bin/env node
/**
 * Setup Verification Script for African Music Pact
 * 
 * Run this script to verify your local environment is correctly configured
 * to connect to Neon database and Vercel Blob storage.
 * 
 * Usage: node scripts/verify-setup.js
 */

const { neon } = require('@neondatabase/serverless');

console.log(`
╔══════════════════════════════════════════════════════════════╗
║         African Music Pact - Setup Verification              ║
╚══════════════════════════════════════════════════════════════╝
`);

// Check environment variables
const checks = {
  database: false,
  blob: false,
  jwt: false,
};

// 1. Check Database URL
const dbUrl = process.env.DATABASE_URL || process.env.POSTGRES_URL;
if (dbUrl) {
  console.log('✓ Database URL found');
  if (dbUrl.includes('neon.tech')) {
    console.log('  → Using Neon PostgreSQL');
  }
  checks.database = true;
} else {
  console.log('✗ Database URL NOT found');
  console.log('  → Set POSTGRES_URL or DATABASE_URL in .env.local');
  console.log('  → Get from: Vercel Project Settings or console.neon.tech');
}

// 2. Check Blob Token
if (process.env.BLOB_READ_WRITE_TOKEN) {
  console.log('✓ Blob storage token found');
  checks.blob = true;
} else {
  console.log('✗ Blob storage token NOT found');
  console.log('  → Set BLOB_READ_WRITE_TOKEN in .env.local');
  console.log('  → Get from: Vercel Dashboard > Storage > Blob');
}

// 3. Check JWT Secret
if (process.env.JWT_SECRET) {
  console.log('✓ JWT secret found');
  checks.jwt = true;
} else {
  console.log('⚠ JWT secret NOT found (will use default - not secure for production)');
}

console.log('');

// 4. Test Database Connection
if (checks.database) {
  console.log('Testing database connection...');
  
  const sql = neon(dbUrl);
  
  sql`SELECT 1 as connected`
    .then(() => {
      console.log('✓ Database connection successful!');
      
      // Check if users table exists
      return sql`SELECT COUNT(*) as count FROM users`;
    })
    .then((result) => {
      console.log(`✓ Users table found with ${result[0].count} users`);
      
      // Check for demo accounts
      return sql`SELECT email FROM users WHERE email IN ('admin@musicapp.com', 'artist@musicapp.com', 'user@musicapp.com')`;
    })
    .then((result) => {
      if (result.length > 0) {
        console.log(`✓ Demo accounts found: ${result.map(r => r.email).join(', ')}`);
      } else {
        console.log('⚠ No demo accounts found - you may need to run database setup scripts');
      }
      
      printSummary(checks);
    })
    .catch((err) => {
      console.log('✗ Database connection failed:', err.message);
      printSummary(checks);
    });
} else {
  printSummary(checks);
}

function printSummary(checks) {
  console.log(`
╔══════════════════════════════════════════════════════════════╗
║                        Summary                                ║
╠══════════════════════════════════════════════════════════════╣
║  Database:     ${checks.database ? '✓ Ready' : '✗ Not configured'}                                    
║  Blob Storage: ${checks.blob ? '✓ Ready' : '✗ Not configured'}                                    
║  JWT Secret:   ${checks.jwt ? '✓ Ready' : '⚠ Using default'}                                    
╚══════════════════════════════════════════════════════════════╝
`);

  if (checks.database && checks.blob) {
    console.log('Your local setup is ready! Run: pnpm dev');
  } else {
    console.log('Please configure the missing environment variables in .env.local');
    console.log('See .env.example for details.');
  }
}
