import 'dotenv/config';
import { query } from '../src/db';

async function verifyWaitlistTable() {
  try {
    console.log('Verifying waitlist_leads table...');

    const result = await query(`
      SELECT EXISTS (
        SELECT FROM information_schema.tables 
        WHERE table_schema = 'public' 
        AND table_name = 'waitlist_leads'
      );
    `);

    if (!result.rows[0].exists) {
      console.log('Table waitlist_leads does not exist. Creating it...');
      await query(`
        CREATE TABLE IF NOT EXISTS waitlist_leads (
          id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
          email VARCHAR(255) UNIQUE NOT NULL,
          source VARCHAR(50) DEFAULT 'landing_page',
          referrer TEXT,
          created_at TIMESTAMP DEFAULT NOW()
        );
        CREATE INDEX IF NOT EXISTS idx_waitlist_leads_email ON waitlist_leads(email);
        CREATE INDEX IF NOT EXISTS idx_waitlist_leads_created_at ON waitlist_leads(created_at);
      `);
      console.log('Table waitlist_leads created successfully.');
    } else {
      console.log('Table waitlist_leads already exists.');

      // Check columns just in case
      const columns = await query(`
        SELECT column_name, data_type 
        FROM information_schema.columns 
        WHERE table_name = 'waitlist_leads';
      `);
      console.log('Columns:', columns.rows.map(r => r.column_name).join(', '));
    }

    console.log('Verification complete.');
  } catch (error) {
    console.error('Error verifying waitlist table:', error);
  } finally {
    process.exit(0);
  }
}

verifyWaitlistTable();
