"""ibkr_pit_volume storage (mirrors test_yahoo_volume_pit.py) + ucits_volume_source config flag.

NOTE: `IbkrBroker.historical_daily_volume` needs a live ib-gateway connection (reqHistoricalData over the
wire) — it is integration-only and is NOT unit-tested here. This file covers the storage layer (PIT
first-seen-frozen roundtrip) and the config flag only.
"""
import datetime as dt

from fxhnt.adapters.persistence.forward_nav import ForwardNavRepo
from fxhnt.config import AllocationSettings


def test_snapshot_and_read_ibkr_volumes_first_seen_frozen():
    r = ForwardNavRepo("sqlite://")
    r.migrate()
    at = dt.datetime(2026, 7, 16)
    r.snapshot_ibkr_volumes("CSPX.L", {"2026-07-01": 1000.0, "2026-07-02": 2000.0}, at)
    r.snapshot_ibkr_volumes("CSPX.L", {"2026-07-01": 9999.0, "2026-07-03": 3000.0}, at)  # 07-01 must NOT change
    assert r.ibkr_pit_volumes("CSPX.L") == {"2026-07-01": 1000.0, "2026-07-02": 2000.0, "2026-07-03": 3000.0}


def test_ucits_volume_source_default_yahoo():
    assert AllocationSettings().ucits_volume_source == "yahoo"


def test_ucits_volume_source_env_override(monkeypatch):
    monkeypatch.setenv("FXHNT_ALLOCATION_UCITS_VOLUME_SOURCE", "ibkr")
    assert AllocationSettings().ucits_volume_source == "ibkr"
