import datetime as dt

from fxhnt.adapters.persistence.cockpit_models import StrategyFundingRow
from fxhnt.adapters.persistence.forward_nav import ForwardNavRepo


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


def _row(sid, state):
    return StrategyFundingRow(strategy_id=sid, state=state, label=state, decay_run=0, recovery_run=0,
                              first_pass_date=None, policy_version=2, policy_hash="h", as_of="2026-07-10",
                              computed_at=dt.datetime(2026, 7, 10))


def test_replace_and_read_funding(tmp_path):
    repo = _repo(tmp_path)
    at = dt.datetime(2026, 7, 10)
    repo.replace_funding([_row("a", "funded"), _row("b", "de-funded")], at)
    assert repo.current_funding() == {"a": "funded", "b": "de-funded"}


def test_replace_clears_stale(tmp_path):
    repo = _repo(tmp_path)
    at = dt.datetime(2026, 7, 10)
    repo.replace_funding([_row("a", "funded"), _row("b", "de-funded")], at)
    repo.replace_funding([_row("a", "funded")], at)          # 'b' dropped this run
    assert repo.current_funding() == {"a": "funded"}
