from __future__ import annotations

import json

from src.tools.maker_taker_shadow_report import build_report


def _append(path, row):
    with path.open("a", encoding="utf-8") as handle:
        handle.write(json.dumps(row) + "\n")


def test_report_compares_only_matched_completed_lifecycles(tmp_path):
    _append(tmp_path / "maker_taker_shadow_journal.jsonl", {"event": "maker_pending", "source_window_id": "w1", "coin": "ETH"})
    _append(tmp_path / "maker_taker_shadow_journal.jsonl", {"event": "maker_fill", "source_window_id": "w1", "coin": "ETH"})
    _append(tmp_path / "maker_taker_shadow_journal.jsonl", {"event": "maker_exit", "source_window_id": "w1", "coin": "ETH", "net_pnl_usd": "0.12"})
    _append(tmp_path / "maker_taker_shadow_journal.jsonl", {"event": "maker_pending", "source_window_id": "w2", "coin": "SOL"})
    _append(tmp_path / "maker_taker_shadow_journal.jsonl", {"event": "maker_cancel", "source_window_id": "w2", "coin": "SOL"})
    _append(tmp_path / "trade_journal.jsonl", {"event": "exit", "data_window_id": "w1", "coin": "ETH", "net_pnl_usd": "0.10"})
    report = build_report(tmp_path)
    assert report["submitted"] == 2
    assert report["filled"] == 1
    assert report["fill_rate_pct"] == "50.0"
    assert report["matched_completed"] == 1
    assert report["maker_net_pnl_usd"] == "0.12"
    assert report["market_net_pnl_usd"] == "0.10"
    assert report["maker_advantage_usd"] == "0.02"
    assert report["execution_allowed"] is False
