const { createClient } = require('@supabase/supabase-js');
const fs = require('fs');
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '../.env') });

const supabase = createClient(
    process.env.SUPABASE_URL,
    process.env.SUPABASE_SERVICE_ROLE_KEY
);

async function checkLogs() {
    console.log('--- Checking recent logs from Supabase ---');
    const { data: logs, error } = await supabase
        .from('logs')
        .select('*')
        .order('created_at', { ascending: false })
        .limit(50);

    if (error) {
        console.error('Error fetching logs:', error.message);
        return;
    }

    if (logs && logs.length > 0) {
        logs.reverse().forEach(log => {
            const time = new Date(log.created_at).toLocaleTimeString();
            const levelEmoji = log.level === 'error' ? '🔴' : log.level === 'warn' ? '🟡' : '🔹';
            console.log(`${time} ${levelEmoji} [${log.source}] ${log.event}: ${log.message}`);
            if (log.payload) {
                console.log('   Payload:', JSON.stringify(log.payload));
            }
        });
    } else {
        console.log('No logs found.');
    }
}

checkLogs();
