import { NextResponse } from 'next/server'; import { getFirms } from '@/lib/db'; import { getClients } from '@/lib/clients'; export async function GET() { try { const firms = getFirms(); const clients = getClients(); const state = firms.map((f) => { const client = clients.get(f.id); if (!client || client.accountList.length === 0) { return { firm: f.name, connected: false, accounts: [] }; } const accounts = client.accountList.map((acc) => { const cash = client.accountCashBalances[acc.id] ?? { amount: 0, realizedPnL: 0 }; return { id: acc.id, name: acc.name, active: acc.active, amount: cash.amount, realizedPnL: cash.realizedPnL, daysTraded: client.daysTraded[acc.id] ?? 0, hasPosition: !!client.positions[acc.id], }; }); return { firm: f.name, connected: true, accounts }; }); return NextResponse.json(state); } catch (err) { console.error('[GET /api/state]', err); return NextResponse.json({ error: 'Failed to fetch state' }, { status: 500 }); } }