import { createClient } from '@supabase/supabase-js';
import * as dotenv from 'dotenv';
import path from 'path';

dotenv.config({ path: path.resolve(process.cwd(), '.env') });
const supabase = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_KEY!);

async function checkUserPersistence() {
    const email = 'saravanan.27ge+006@gmail.com';
    console.log(`Checking user: ${email}`);

    const { data: users, error } = await supabase.from('users').select('*').eq('email', email);
    if (error || !users || users.length === 0) {
        console.error('User not found in DB!');
        return;
    }

    const user = users[0];
    const userId = user.user_id;
    console.log(`Found User ID: ${userId}`);

    // Check recent entries for this specific ID
    const { count: orders } = await supabase.from('orders').select('*', { count: 'exact', head: true }).eq('user_id', userId);
    const { count: history } = await supabase.from('trade_history').select('*', { count: 'exact', head: true }).eq('user_id', userId);

    console.log(`Stats for this user: Orders=${orders}, History=${history}`);

    if (orders > 0) {
        const { data } = await supabase.from('orders').select('symbol, side, status, created_at').eq('user_id', userId).order('created_at', { ascending: false }).limit(5);
        console.log('Recent Orders:', JSON.stringify(data, null, 2));
    }
}

checkUserPersistence();
