const { createClient } = require('@supabase/supabase-js');
const fs = require('fs');
require('dotenv').config();

async function applyMigration() {
  const supabase = createClient(
    process.env.SUPABASE_URL,
    process.env.SUPABASE_SERVICE_ROLE_KEY
  );

  const sql = fs.readFileSync('src/backend/database/migrations/001_domain_verifications.sql', 'utf8');
  
  console.log('Applying domain verification schema to Supabase...\n');
  
  // Split by semicolons and execute each statement
  const statements = sql
    .split(';')
    .map(s => s.trim())
    .filter(s => s.length > 0 && !s.startsWith('--'));

  for (const statement of statements) {
    try {
      console.log('Executing:', statement.substring(0, 80) + '...');
      const { data, error } = await supabase.rpc('exec_sql', { sql: statement });
      if (error) {
        console.error('Error:', error.message);
      } else {
        console.log('✓ Success');
      }
    } catch (err) {
      console.log('Note:', err.message);
    }
  }

  console.log('\n✓ Migration complete! Domain verification tables are ready.');
  process.exit(0);
}

applyMigration();
