from typing import Any

from fastapi import APIRouter

from connectors.registry import connector_registry

router = APIRouter(prefix="/platforms", tags=["platforms"])


@router.get("")
def list_platforms() -> dict[str, Any]:
    """Vue d'ensemble des connecteurs — actifs et planifies."""
    trading = connector_registry.trading_platforms()
    manual_ok = [s for s in trading if s.discipline_manual]
    auto_ok = [s for s in trading if s.discipline_auto]
    connected = [s for s in trading if s.connected]
    return {
        "product": "Paychek Safeguard",
        "trading_platform_count": len(trading),
        "discipline_manual_count": len(manual_ok),
        "discipline_auto_count": len(auto_ok),
        "connected_count": len(connected),
        "platforms": [s.model_dump(mode="json") for s in trading],
    }


@router.get("/{platform}/status")
def platform_status(platform: str) -> dict[str, Any]:
    connector = connector_registry.get(platform)
    if connector is None:
        return {"ok": False, "error": "unknown_platform"}
    return {"ok": True, **connector.status().model_dump(mode="json")}
