from __future__ import annotations

import asyncio
from typing import Iterable, Awaitable

from app.config import settings
from app.streaming.binance_hub import hub as binance_hub
from app.streaming.data_provider import data_provider


async def bootstrap_streams() -> None:
    """Ensure configured streams are hot even before clients connect."""
    if not settings.STREAM_AUTO_BOOTSTRAP:
        return

    symbols: Iterable[str] = settings.STREAM_WARM_SYMBOLS or []
    timeframe = settings.STREAM_WARM_TIMEFRAME or "1m"
    coros: list[Awaitable[None]] = []

    for raw in symbols:
        sym = (raw or "").strip()
        if not sym:
            continue
        if sym.upper().startswith("XAU"):
            coros.append(data_provider.ensure_stream(sym, timeframe))
        else:
            coros.append(binance_hub.ensure_stream(sym, timeframe))

    if coros:
        await asyncio.gather(*coros, return_exceptions=True)