"""
=========================================
CV6 AI Trading OS — Trading State API
=========================================
Read-only HTTP surface over the single source of truth. Every consumer
that needs "what does the account actually hold / how has it performed"
should read from here (or from dashboard_service, which now delegates
here), never from a module-local copy.
"""

from fastapi import APIRouter, Depends
from app.core.dependencies import get_current_user
from app.state.trading_state_manager import trading_state_manager

router = APIRouter(prefix="/state", tags=["Trading State"])


@router.get("/", summary="Full trading-state snapshot (single source of truth)")
def full_state(_user=Depends(get_current_user)):
    return trading_state_manager.snapshot()


@router.get("/positions", summary="All open positions (authoritative)")
def positions(_user=Depends(get_current_user)):
    snap = trading_state_manager.snapshot()
    return {"success": True, "positions": snap["positions"], "count": snap["position_count"]}


@router.get("/capital", summary="Capital: total / available / deployed")
def capital(_user=Depends(get_current_user)):
    return trading_state_manager.snapshot()["capital"]


@router.get("/pnl", summary="Realized + unrealized PnL (authoritative)")
def pnl(_user=Depends(get_current_user)):
    return trading_state_manager.snapshot()["pnl"]


@router.get("/orders", summary="Tracked orders")
def orders(_user=Depends(get_current_user)):
    snap = trading_state_manager.snapshot()
    return {"success": True, "orders": snap["orders"]}


@router.get("/version", summary="State version (increments on every change)")
def version(_user=Depends(get_current_user)):
    return {"version": trading_state_manager.version()}
