-- Equity history for live account performance dashboards.

CREATE TABLE IF NOT EXISTS live_account_equity_history (
    id BIGSERIAL PRIMARY KEY,
    account_id BIGINT NOT NULL REFERENCES live_trade_accounts(id) ON DELETE CASCADE,
    equity_usdt DOUBLE PRECISION NOT NULL DEFAULT 0,
    wallet_balance_usdt DOUBLE PRECISION NOT NULL DEFAULT 0,
    available_usdt DOUBLE PRECISION NOT NULL DEFAULT 0,
    used_margin_usdt DOUBLE PRECISION NOT NULL DEFAULT 0,
    unrealized_pnl_usdt DOUBLE PRECISION NOT NULL DEFAULT 0,
    open_position_value_usdt DOUBLE PRECISION NOT NULL DEFAULT 0,
    position_count INTEGER NOT NULL DEFAULT 0,
    snapshot_at TEXT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_live_account_equity_history_account_time
    ON live_account_equity_history(account_id, snapshot_at DESC);
