#!/bin/bash
cd /var/www/sneakerpicks

# Check supabase env for DB password
grep -E 'POSTGRES_PASSWORD|DATABASE_URL|DB_' /opt/supabase/docker/.env 2>/dev/null | head -10

# Try connecting via pooler with postgres user and the supabase password
SUPABASE_PASS=$(grep 'POSTGRES_PASSWORD' /opt/supabase/docker/.env 2>/dev/null | cut -d= -f2)
echo "Supabase postgres password: $SUPABASE_PASS"

# Try direct connection to DB container
node -e "
const pg = require('pg');
const pool = new pg.Pool({ connectionString: 'postgresql://postgres:${SUPABASE_PASS}@localhost:5432/sneakerpicks' });
pool.query('SELECT COUNT(*) FROM products').then(r => console.log('Direct Products:', r.rows[0].count)).catch(e => console.error('Direct Error:', e.message)).finally(() => pool.end());
"

# Try port 6543 (transaction mode pooler)
node -e "
const pg = require('pg');
const pool = new pg.Pool({ connectionString: 'postgresql://postgres.sneakerpicks:${SUPABASE_PASS}@localhost:6543/sneakerpicks' });
pool.query('SELECT COUNT(*) FROM products').then(r => console.log('Pooler Products:', r.rows[0].count)).catch(e => console.error('Pooler Error:', e.message)).finally(() => pool.end());
"
