"""Bulk ingest: many symbols written in ONE transaction (perf), same result as per-symbol writes."""
from __future__ import annotations

from fxhnt.adapters.warehouse.duckdb_feature_store import DuckDbFeatureStore
from fxhnt.application.warehouse_ingest import WarehouseIngest
from fxhnt.domain.warehouse.bar import BarRecord


def test_write_features_bulk_round_trip_and_idempotent(tmp_path) -> None:
    store = DuckDbFeatureStore(str(tmp_path / "wh.duckdb"))
    items = [("BTCUSDT", [(0, {"close": 100.0}), (86_400, {"close": 110.0})]),
             ("ETHUSDT", [(86_400, {"close": 50.0})])]
    store.write_features_bulk(items)
    assert store.read_features("BTCUSDT", features=["close"]) == [(0, {"close": 100.0}), (86_400, {"close": 110.0})]
    assert store.read_features("ETHUSDT", features=["close"]) == [(86_400, {"close": 50.0})]
    store.write_features_bulk(items)                       # idempotent: re-write same range -> no dupes
    assert len(store.read_features("BTCUSDT", features=["close"])) == 2
    store.close()


def test_ingest_bars_bulk_matches_per_symbol(tmp_path) -> None:
    store = DuckDbFeatureStore(str(tmp_path / "wh.duckdb"))
    bulk = {"BTCUSDT": [BarRecord("BTCUSDT", 0, 100.0, 101.0, 99.0)],
            "ETHUSDT": [BarRecord("ETHUSDT", 0, 50.0, 51.0, 49.0)]}
    n = WarehouseIngest(store).ingest_bars_bulk(bulk)
    assert n == 2
    assert store.read_features("BTCUSDT", features=["close", "high", "low"])[0][1] == {"close": 100.0, "high": 101.0, "low": 99.0}
    store.close()
