/**
 * Test Supabase Connection from Docker Environment
 */

import { createClient } from '@supabase/supabase-js';

const supabaseUrl = 'https://zctjaivtfyfxokfaemek.supabase.co';
const supabaseAnonKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InpjdGphaXZ0ZnlmeG9rZmFlbWVrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTA4NjU1MjEsImV4cCI6MjA2NjQ0MTUyMX0.IBgyGY7WzLL77ru-_JtThSdAnXFmsNLkKdvK0omGssY';

const supabase = createClient(supabaseUrl, supabaseAnonKey);

console.log('Testing Supabase connection...');
console.log('URL:', supabaseUrl);
console.log('Key (first 20 chars):', supabaseAnonKey.substring(0, 20) + '...');

// Test authentication
async function testLogin() {
  try {
    const { data, error } = await supabase.auth.signInWithPassword({
      email: 'tmartinez@gmail.com',
      password: 'Skittles@420',
    });

    console.log('\n--- Login Test Results ---');
    if (error) {
      console.log('❌ Login failed:', error.message);
      console.log('Error details:', error);
    } else {
      console.log('✅ Login successful!');
      console.log('User ID:', data.user?.id);
      console.log('User email:', data.user?.email);
      console.log('Session access token (first 20 chars):', data.session?.access_token?.substring(0, 20) + '...');
    }
  } catch (err) {
    console.error('❌ Connection error:', err);
  }
}

// Test basic connection
async function testConnection() {
  try {
    // Try to fetch from a public table or make a basic query
    const { data, error } = await supabase
      .from('events')
      .select('count')
      .limit(1);
    
    console.log('\n--- Connection Test Results ---');
    if (error) {
      console.log('❌ Connection failed:', error.message);
    } else {
      console.log('✅ Connection successful!');
      console.log('Query result:', data);
    }
  } catch (err) {
    console.error('❌ Connection error:', err);
  }
}

// Run tests
await testConnection();
await testLogin();