"""Bronze->silver->warehouse ingest helpers for the orchestration assets + tests (one place so the asset
and the equivalence test ingest identically)."""
from __future__ import annotations

import glob
import os

import numpy as np

from fxhnt.adapters.warehouse.silver_crypto import normalize_crypto
from fxhnt.application.warehouse_ingest import WarehouseIngest
from fxhnt.ports.warehouse import FeatureStore


def ingest_crypto_pit(store: FeatureStore, crypto_dir: str) -> int:
    """Ingest every crypto_pit/<SYM>.npz (keys day, close) into the warehouse via silver_crypto.
    All symbols are written in ONE transaction (bulk) — ~50x faster than per-symbol on a network PVC."""
    symbol_to_bars = {}
    for path in sorted(glob.glob(os.path.join(crypto_dir, "*.npz"))):
        sym = os.path.splitext(os.path.basename(path))[0]
        with np.load(path) as z:
            symbol_to_bars[sym] = normalize_crypto(sym, z["day"], z["close"])
    return WarehouseIngest(store).ingest_bars_bulk(symbol_to_bars)
