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

const supabaseUrl = process.env.PUBLIC_SUPABASE_URL;
const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY;

if (!supabaseUrl || !supabaseServiceKey) {
  console.error('❌ Missing required environment variables');
  process.exit(1);
}

const supabase = createClient(supabaseUrl, supabaseServiceKey, {
  auth: {
    autoRefreshToken: false,
    persistSession: false
  }
});

async function verifySchema() {
  console.log('🔍 Verifying database schema...');
  
  try {
    // Check if tables exist and what columns they have
    const eventId = '7ac12bd2-8509-4db3-b1bc-98a808646311';
    
    console.log('\n1️⃣ Checking events table...');
    const { data: eventData, error: eventError } = await supabase
      .from('events')
      .select('*')
      .eq('id', eventId)
      .single();
    
    if (eventError) {
      console.error('❌ Event query error:', eventError);
    } else {
      console.log('✅ Event found:', eventData.title);
      console.log('Columns:', Object.keys(eventData));
    }
    
    console.log('\n2️⃣ Checking ticket_types table...');
    const { data: ticketTypesData, error: ticketTypesError } = await supabase
      .from('ticket_types')
      .select('*')
      .eq('event_id', eventId)
      .limit(1);
    
    if (ticketTypesError) {
      console.error('❌ Ticket types query error:', ticketTypesError);
    } else {
      console.log(`✅ Found ${ticketTypesData.length} ticket types`);
      if (ticketTypesData.length > 0) {
        console.log('Columns:', Object.keys(ticketTypesData[0]));
        console.log('Sample:', ticketTypesData[0]);
      }
    }
    
    console.log('\n3️⃣ Checking tickets table...');
    const { data: ticketsData, error: ticketsError } = await supabase
      .from('tickets')
      .select('*')
      .eq('event_id', eventId)
      .limit(1);
    
    if (ticketsError) {
      console.error('❌ Tickets query error:', ticketsError);
    } else {
      console.log(`✅ Found ${ticketsData.length} tickets`);
      if (ticketsData.length > 0) {
        console.log('Columns:', Object.keys(ticketsData[0]));
        console.log('Sample:', ticketsData[0]);
      }
    }
    
    console.log('\n4️⃣ Testing the exact stats query...');
    
    // Test the exact query from the stats API
    const { data: testTickets, error: testError } = await supabase
      .from('tickets')
      .select(`
        id,
        ticket_type_id,
        price,
        checked_in,
        scanned_at,
        created_at
      `)
      .eq('event_id', eventId);
    
    if (testError) {
      console.error('❌ Stats query error:', testError);
    } else {
      console.log(`✅ Stats query successful: ${testTickets.length} tickets`);
      if (testTickets.length > 0) {
        console.log('Sample ticket:', testTickets[0]);
      }
    }
    
    console.log('\n5️⃣ Testing ticket types query...');
    const { data: testTicketTypes, error: testTicketTypesError } = await supabase
      .from('ticket_types')
      .select('id, quantity_available, price, name')
      .eq('event_id', eventId);
    
    if (testTicketTypesError) {
      console.error('❌ Ticket types stats query error:', testTicketTypesError);
    } else {
      console.log(`✅ Ticket types query successful: ${testTicketTypes.length} types`);
      if (testTicketTypes.length > 0) {
        console.log('Sample type:', testTicketTypes[0]);
      }
    }
    
  } catch (error) {
    console.error('💥 Unexpected error:', error);
  }
}

verifySchema();