import datetime as dt

from fxhnt.adapters.persistence.cockpit_models import StrategyAllocationRow, 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 test_all_allocations_and_funding_roundtrip(tmp_path):
    repo = _repo(tmp_path)
    at = dt.datetime(2026, 7, 10)
    repo.replace_allocations([StrategyAllocationRow(strategy_id="bybit_4edge", target_weight=0.54,
        target_dollars=18900.0, policy_version=1, policy_hash="h", as_of="2026-07-10", computed_at=at)], at)
    repo.replace_funding([StrategyFundingRow(strategy_id="bybit_4edge", state="funded", label="funded",
        decay_run=0, recovery_run=0, first_pass_date=None, policy_version=2, policy_hash="h",
        as_of="2026-07-10", computed_at=at)], at)
    allocs = {a.strategy_id: a for a in repo.all_allocations()}
    funds = {f.strategy_id: f for f in repo.all_funding()}
    assert allocs["bybit_4edge"].target_dollars == 18900.0
    assert allocs["bybit_4edge"].target_weight == 0.54
    assert funds["bybit_4edge"].label == "funded"


def test_reads_empty_when_absent(tmp_path):
    repo = _repo(tmp_path)
    assert repo.all_allocations() == []
    assert repo.all_funding() == []
