import { createClient } from '@supabase/supabase-js';
import * as dotenv from 'dotenv';
dotenv.config();

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

async function test() {
    const { data: users } = await supabase.from('users').select('user_id, email').limit(1);
    if (!users || users.length === 0) {
        console.error("No users found");
        return;
    }
    const userId = users[0].user_id;
    console.log(`Using User ID: ${userId} (${users[0].email})`);

    const testProfileName = `TEST_HOT_LOAD_${Date.now()}`;

    console.log(`\n--- 1. Testing INSERT (Dynamic Load) ---`);
    const { data: insertData, error: insError } = await supabase.from('trade_profiles').insert([{
        user_id: userId,
        name: testProfileName,
        allocated_capital: 555,
        risk_per_trade_percent: 0.5,
        symbols: 'BTC/USDT, ETH/USDT',
        is_active: true
    }]).select();

    if (insError) {
        console.error("Insert error:", insError);
        return;
    }
    const profileId = insertData[0].id;
    console.log(`✅ Profile created: ${profileId}. Bot should log [HOT-LOAD]...`);

    // Wait for bot to pick it up
    await new Promise(r => setTimeout(r, 5000));

    console.log(`\n--- 2. Testing UPDATE (Hot Swap) ---`);
    const { error: updError } = await supabase.from('trade_profiles')
        .update({ allocated_capital: 999 })
        .eq('id', profileId);

    if (updError) console.error("Update error:", updError);
    else console.log(`✅ Capital updated to 999. Bot should log [HOT-REMOVE] then [HOT-LOAD]...`);

    await new Promise(r => setTimeout(r, 5000));

    console.log(`\n--- 3. Testing DELETE (Hot Remove) ---`);
    const { error: delError } = await supabase.from('trade_profiles')
        .delete()
        .eq('id', profileId);

    if (delError) console.error("Delete error:", delError);
    else console.log(`✅ Profile deleted. Bot should log [HOT-REMOVE]...`);
}

test();
