import datetime as dt

from fxhnt.adapters.persistence.forward_nav import ForwardNavRepo


def _repo(tmp_path):
    repo = ForwardNavRepo(f"sqlite:///{tmp_path}/pit.db")
    repo.migrate()
    return repo


def test_snapshot_is_first_seen_wins(tmp_path):
    repo = _repo(tmp_path)
    at = dt.datetime(2026, 7, 8)
    repo.snapshot_yahoo_closes("SPY", {"2026-07-07": 500.0, "2026-07-08": 501.0}, at)
    # a later re-fetch REVISES 07-07 (dividend adj) + adds a new day
    repo.snapshot_yahoo_closes("SPY", {"2026-07-07": 499.5, "2026-07-08": 501.0, "2026-07-09": 502.0}, at)
    closes = repo.yahoo_pit_closes("SPY")
    assert closes["2026-07-07"] == 500.0     # FROZEN at first-seen, not revised
    assert closes["2026-07-09"] == 502.0     # new day appended


def test_pit_client_reads_frozen_series(tmp_path):
    from fxhnt.adapters.data.yahoo_pit import YahooPitClient
    repo = _repo(tmp_path)
    repo.snapshot_yahoo_closes("SPY", {"2026-07-07": 500.0}, dt.datetime(2026, 7, 8))
    assert YahooPitClient(repo).adj_closes("SPY") == {"2026-07-07": 500.0}
