#!/usr/bin/env node
/**
 * Run SQL migration in Supabase
 *
 * This script requires direct PostgreSQL connection to Supabase.
 * Get your database password from Supabase Dashboard:
 * Settings → Database → Connection string → Password
 *
 * Usage:
 *   PGPASSWORD=your_password node run-migration.js
 */

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

const SUPABASE_URL = process.env.SUPABASE_URL || 'https://bfzizknbxbsfrffqityf.supabase.co';
const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY || 'sb_secret_N7TN930UzzXGgFgrAaozqA_Y4b0DKlM';

const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);

async function runMigration() {
  console.log('Checking if password_resets table exists...');

  // Try to query the table
  const { data, error } = await supabase
    .from('password_resets')
    .select('id')
    .limit(1);

  if (!error) {
    console.log('✓ password_resets table already exists');
    return;
  }

  if (error.code === 'PGRST205') {
    console.log('Table does not exist. Please run the following SQL in Supabase Dashboard:');
    console.log('\n--- SQL to execute in Supabase Dashboard (SQL Editor) ---\n');

    const sql = fs.readFileSync(
      path.join(__dirname, 'migrations/001_password_resets.sql'),
      'utf8'
    );
    console.log(sql);
    console.log('\n--- End of SQL ---\n');
    console.log('URL: https://supabase.com/dashboard/project/bfzizknbxbsfrffqityf/sql');
  } else {
    console.error('Error:', error);
  }
}

runMigration().catch(console.error);
