"""Ingest walks the registry, reads each present state file, computes the gate, and upserts rows + summary.
A missing or malformed state file is skipped (logged), never aborts the batch."""
from __future__ import annotations

import datetime as dt
import json

from fxhnt.adapters.persistence.forward_nav import ForwardNavRepo
from fxhnt.application.forward_ingest import ForwardIngestService


def _setup(tmp_path):
    (tmp_path / "fxhnt_combined_forward.json").write_text(json.dumps({
        "inception": "2026-06-04", "last_date": "2026-06-06", "nav": 1.0201,
        "days": [{"date": "2026-06-05", "ret": 0.01, "nav": 1.01},
                 {"date": "2026-06-06", "ret": 0.0099, "nav": 1.0201}],
    }))
    (tmp_path / "multistrat_state.json").write_text(json.dumps({
        "last_date": "2026-06-12", "days": 5, "sum_r": -0.0007, "sumsq_r": 1.88e-05,
        "equity": 0.99926, "peak": 1.0014, "max_dd": -0.0046,
    }))
    repo = ForwardNavRepo("sqlite://")
    repo.migrate()
    return repo


def test_ingest_populates_summaries_and_rows(tmp_path) -> None:
    repo = _setup(tmp_path)
    svc = ForwardIngestService(repo, str(tmp_path))
    n = svc.ingest(at=dt.datetime(2026, 6, 14, 23, 30))
    assert n == 2                                              # two present trackers ingested
    summaries = {r.strategy_id: r for r in repo.all_summaries()}
    assert "combined" in summaries and "multistrat" in summaries
    assert summaries["combined"].gate_status == "WAIT"        # 2 < 20 days
    assert len(repo.nav_history("combined")) == 2


def test_ingest_skips_missing_files(tmp_path) -> None:
    repo = _setup(tmp_path)
    (tmp_path / "multistrat_state.json").unlink()             # now only combined present
    svc = ForwardIngestService(repo, str(tmp_path))
    n = svc.ingest(at=dt.datetime(2026, 6, 14, 23, 30))
    assert n == 1


def test_ingest_skips_malformed_file(tmp_path) -> None:
    repo = _setup(tmp_path)
    (tmp_path / "multistrat_state.json").write_text("{ not json")
    svc = ForwardIngestService(repo, str(tmp_path))
    n = svc.ingest(at=dt.datetime(2026, 6, 14, 23, 30))       # malformed skipped, combined still ingested
    assert n == 1
