"""Run on a scheduler. Failures are reported per source so one outage does not stop all data."""
from app.services.market_data import persist_quote
from app.sources.fx_api import FxApiSource
from app.sources.angel_one import AngelOneSource
from app.sources.upstox import UpstoxSource
from app.core.settings import get_settings


def main() -> None:
    sources = [(UpstoxSource(), "GIFT NIFTY")]
    settings = get_settings()
    if settings.angel_api_key:
        sources += [(AngelOneSource(), symbol) for symbol in settings.market_symbols]
    sources += [(FxApiSource(), pair) for pair in settings.fx_pairs]
    for source, symbol in sources:
        try:
            persist_quote(source.latest(symbol))
            print(f"saved {symbol}")
        except Exception as exc:
            print(f"FAILED {symbol}: {exc}")


if __name__ == "__main__":
    main()
