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

// Supabase configuration from environment
const SUPABASE_URL = 'https://gzoclmbqmgmpuhufbnhy.supabase.co';
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imd6b2NsbWJxbWdtcHVodWZibmh5Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTM4MTY2NzgsImV4cCI6MjA2OTM5MjY3OH0.Jg8cAKbujDv7YgeLCeHsOkgkP-LwM-7fAXVIHno0pLI';
const SUPABASE_SERVICE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imd6b2NsbWJxbWdtcHVodWZibmh5Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc1MzgxNjY3OCwiZXhwIjoyMDY5MzkyNjc4fQ.f9PUzL1F8JqIkqD_DwrGBIyHPcehMo-97jXD8hee5ss';

async function testSupabaseClient() {
  console.log('Testing Supabase client connection...');
  
  try {
    // Test with anon key
    console.log('\n🔍 Testing with anon key...');
    const anonClient = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
    
    // Test a simple query
    const { data: anonData, error: anonError } = await anonClient
      .from('users')
      .select('*')
      .limit(1);
    
    if (anonError) {
      console.log(`❌ Anon client error: ${anonError.message}`);
    } else {
      console.log(`✅ Anon client working! Found ${anonData?.length || 0} users`);
    }
    
    // Test with service key
    console.log('\n🔍 Testing with service key...');
    const serviceClient = createClient(SUPABASE_URL, SUPABASE_SERVICE_KEY);
    
    // Test a simple query
    const { data: serviceData, error: serviceError } = await serviceClient
      .from('users')
      .select('*')
      .limit(1);
    
    if (serviceError) {
      console.log(`❌ Service client error: ${serviceError.message}`);
    } else {
      console.log(`✅ Service client working! Found ${serviceData?.length || 0} users`);
    }
    
    // Test if documents table exists
    console.log('\n🔍 Testing documents table...');
    const { data: docsData, error: docsError } = await serviceClient
      .from('documents')
      .select('*')
      .limit(1);
    
    if (docsError) {
      console.log(`❌ Documents table error: ${docsError.message}`);
      if (docsError.message.includes('relation "documents" does not exist')) {
        console.log('📋 Documents table does not exist - this is the issue!');
      }
    } else {
      console.log(`✅ Documents table exists! Found ${docsData?.length || 0} documents`);
    }
    
    // List all tables
    console.log('\n🔍 Listing all tables...');
    const { data: tablesData, error: tablesError } = await serviceClient
      .rpc('get_tables');
    
    if (tablesError) {
      console.log(`❌ Could not list tables: ${tablesError.message}`);
      
      // Try a different approach to list tables
      const { data: schemaData, error: schemaError } = await serviceClient
        .from('information_schema.tables')
        .select('table_name')
        .eq('table_schema', 'public');
      
      if (schemaError) {
        console.log(`❌ Could not query schema: ${schemaError.message}`);
      } else {
        console.log(`✅ Found tables: ${schemaData?.map(t => t.table_name).join(', ') || 'none'}`);
      }
    } else {
      console.log(`✅ Tables: ${tablesData?.join(', ') || 'none'}`);
    }
    
  } catch (error) {
    console.error('❌ Supabase client test failed:', error.message);
    console.error('Error details:', error);
  }
}

testSupabaseClient(); 