import { createClient } from '@supabase/supabase-js';
import { config } from '../src/config/index.js';

async function auditProfileMapping() {
    if (!config.SUPABASE_URL || !config.SUPABASE_KEY) {
        return;
    }

    const supabase = createClient(config.SUPABASE_URL, config.SUPABASE_KEY);

    const { data: profiles } = await supabase.from('trade_profiles').select('*');
    const { data: history } = await supabase.from('trade_history').select('*');
    const { data: orders } = await supabase.from('orders').select('*');

    const audit: any = {};
    profiles?.forEach(p => {
        audit[p.id] = {
            name: p.name,
            capital: p.allocated_capital,
            trades: history?.filter(h => h.profile_id === p.id).length || 0,
            orders: orders?.filter(o => o.profile_id === p.id).length || 0,
            pnl: history?.filter(h => h.profile_id === p.id).reduce((sum, h) => sum + Number(h.pnl || 0), 0)
        };
    });

    console.log(JSON.stringify({
        profiles: audit,
        orphanedTrades: history?.filter(h => !h.profile_id).length || 0,
        orphanedOrders: orders?.filter(o => !o.profile_id).length || 0
    }, null, 2));
}

auditProfileMapping().catch(console.error);
