"""WarehousePriceProvider reads bars from the SSOT as a PriceSeries (DataProvider port)."""
from __future__ import annotations

import numpy as np

from fxhnt.adapters.warehouse.duckdb_feature_store import DuckDbFeatureStore
from fxhnt.adapters.warehouse.price_provider import WarehousePriceProvider
from fxhnt.application.warehouse_ingest import WarehouseIngest
from fxhnt.domain.models import AssetClass, Market
from fxhnt.domain.warehouse.bar import BarRecord

_M = Market(symbol="ES", asset_class=AssetClass.FUTURE)


def _store(tmp_path):
    store = DuckDbFeatureStore(str(tmp_path / "wh.duckdb"))
    bars = [BarRecord("ES", 1_577_836_800, 100.0, 101.0, 99.0),     # 2020-01-01
            BarRecord("ES", 1_577_923_200, 102.0, 103.0, 101.0),    # 2020-01-02
            BarRecord("ES", 1_578_009_600, 104.0, 105.0, 103.0)]    # 2020-01-03
    WarehouseIngest(store).ingest_bars("ES", bars)
    return store


def test_fetch_returns_priceseries_round_trip(tmp_path) -> None:
    ps = WarehousePriceProvider(_store(tmp_path)).fetch(_M)
    assert list(ps.dates) == ["2020-01-01", "2020-01-02", "2020-01-03"]
    assert list(ps.close) == [100.0, 102.0, 104.0]
    assert list(ps.high) == [101.0, 103.0, 105.0]
    assert list(ps.low) == [99.0, 101.0, 103.0]
    assert ps.market == _M


def test_fetch_respects_start_end_no_lookahead(tmp_path) -> None:
    ps = WarehousePriceProvider(_store(tmp_path)).fetch(_M, start="2020-01-02", end="2020-01-02")
    assert list(ps.dates) == ["2020-01-02"]            # bars outside [start,end] excluded
    assert list(ps.close) == [102.0]


def test_fetch_raises_when_symbol_absent(tmp_path) -> None:
    import pytest
    with pytest.raises(RuntimeError):
        WarehousePriceProvider(_store(tmp_path)).fetch(Market(symbol="NQ", asset_class=AssetClass.FUTURE))
