import datetime as dt

from fxhnt.adapters.persistence.forward_nav import ForwardNavRepo
from fxhnt.application.multistrat_exec_record import record_exec_track


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


def test_first_run_inceptions_no_return_then_books_second_day(tmp_path):
    repo = _repo(tmp_path)
    at = dt.datetime(2026, 7, 9, 23, 30)
    # run 1: exec_return None (inception) — stores position-state, books no return
    record_exec_track(repo, exec_return=None, new_positions={"SPY": 20.0},
                      today_prices={"SPY": 500.0}, envelope=10_000.0, today="2026-07-09", at=at)
    assert repo.record_series("multistrat_exec") == []                # inception: nothing booked
    assert repo.get_book_state("multistrat_exec_pos")["envelope"] == 10_000.0
    # run 2: a +2% executed day
    record_exec_track(repo, exec_return=0.02, new_positions={"SPY": 20.4},
                      today_prices={"SPY": 510.0}, envelope=10_200.0, today="2026-07-10", at=at)
    assert repo.record_series("multistrat_exec") == [("2026-07-10", 0.02)]   # observed return booked
    assert [r.date for r in repo.nav_history("multistrat_exec")] == ["2026-07-10"]  # nav derived (via run_track)
    assert repo.get_book_state("multistrat_exec_pos")["positions"] == {"SPY": 20.4}  # state advanced
