"""Fetch and cache all raw data for the project.

Run this to refresh the committed offline caches::

    python data/fetch.py

It downloads:
  * daily adjusted close for the configured universe (Yahoo Finance)  -> data/prices.csv.gz
  * daily Fama-French 3 factors + momentum + RF (Ken French library)  -> data/ff_factors.csv.gz

The repository ships with these caches already populated so that
``python run.py`` works with no network access.  ``run.py`` only hits the
network if a cache file is missing or ``--refresh`` is passed.
"""
from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from src import config, data  # noqa: E402


def main() -> None:
    config.ensure_dirs()
    print(f"Fetching {len(config.UNIVERSE)} tickers "
          f"{config.START} -> {config.END} from Yahoo Finance ...")
    prices = data.fetch_prices(list(config.UNIVERSE))
    prices.to_csv(config.PRICE_CACHE)
    print(f"  saved {prices.shape[1]} tickers x {prices.shape[0]} days "
          f"-> {config.PRICE_CACHE.name}")

    print("Fetching Fama-French daily factors + momentum from Ken French ...")
    factors = data.fetch_ff_factors()
    factors.to_csv(config.FACTOR_CACHE)
    print(f"  saved {factors.shape[0]} days x {factors.shape[1]} factors "
          f"-> {config.FACTOR_CACHE.name}")


if __name__ == "__main__":
    main()
