// scripts/reset-database.ts
// Script to completely reset the database (drop all tables)

import { Pool } from '@neondatabase/serverless';
import * as dotenv from 'dotenv';

// Load environment variables from .env.local
dotenv.config({ path: '.env.local' });

async function resetDatabase() {
  console.log('🗑️  Starting database reset...\n');

  if (!process.env.DATABASE_URL) {
    console.error('❌ ERROR: DATABASE_URL not found in environment variables');
    process.exit(1);
  }

  const pool = new Pool({ connectionString: process.env.DATABASE_URL });

  try {
    console.log('📡 Connecting to database...');
    await pool.query('SELECT 1');
    console.log('✅ Connected\n');

    // Drop all tables
    console.log('🔨 Dropping all tables...');
    
    await pool.query('DROP TABLE IF EXISTS blog_posts CASCADE');
    console.log('✅ Dropped blog_posts table');
    
    await pool.query('DROP TABLE IF EXISTS articles CASCADE');
    console.log('✅ Dropped articles table');
    
    await pool.query('DROP TABLE IF EXISTS categories CASCADE');
    console.log('✅ Dropped categories table');
    
    await pool.query('DROP TABLE IF EXISTS newsletter_subscribers CASCADE');
    console.log('✅ Dropped newsletter_subscribers table');

    console.log('\n✅ Database reset complete!');
    console.log('📝 Run "npm run db:migrate" to recreate tables\n');

  } catch (error) {
    console.error('\n❌ ERROR during reset:');
    console.error(error);
    process.exit(1);
  } finally {
    await pool.end();
  }
}

resetDatabase();
