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!);

const userId = '8d5efd9e-0760-4859-8c07-0930ab3ede5a';

async function testVerboseInsert() {
    console.log(`Attempting insert for ${userId}...`);

    // 1. Try Order
    const orderData = {
        user_id: userId,
        symbol: 'BTC/USD',
        type: 'Market',
        side: 'buy',
        qty: 0.1,
        price: 90000,
        status: 'Filled',
        timestamp: Date.now()
    };

    console.log('Inserting into orders...');
    const { data: oData, error: oErr } = await supabase.from('orders').insert([orderData]).select();
    if (oErr) console.error('Orders Insert Failed:', oErr);
    else console.log('Orders Insert OK:', oData[0].user_id);

    // 2. Try Trade History
    const historyData = {
        user_id: userId,
        symbol: 'BTC/USD',
        side: 'buy',
        entry_price: 90000,
        exit_price: 91000,
        size: 0.1,
        pnl: 100,
        pnl_percent: 1.1,
        reason: 'VERBOSE_TEST',
        timestamp: Date.now()
    };

    console.log('Inserting into trade_history...');
    const { data: hData, error: hErr } = await supabase.from('trade_history').insert([historyData]).select();
    if (hErr) console.error('History Insert Failed:', hErr);
    else console.log('History Insert OK:', hData[0].user_id);
}

testVerboseInsert();
