"""Lightweight regression checks for the live-mode wire protocol parser.
Not pytest-based (matches the rest of this project's tests/) -- run
directly: `python tests/test_live_protocol.py`. Exits non-zero on failure.
"""

from __future__ import annotations

import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from x4_advisor.live.protocol import parse_snapshot_lines, to_economy_summary  # noqa: E402


def check(label, condition):
    status = "ok" if condition else "FAIL"
    print(f"[{status}] {label}")
    if not condition:
        raise SystemExit(1)


def test_full_snapshot():
    lines = ["MONEY 10239878892", "SHIP ship_l", "SHIP ship_m", "SHIP ship_m", "STATION", "END"]
    snap = parse_snapshot_lines(lines)
    check("money parsed", snap.money_raw == 10239878892)
    check("ship counts", snap.ship_counts == {"ship_l": 1, "ship_m": 2})
    check("station count", snap.station_count == 1)
    check("got END", snap.got_end is True)
    check("no unrecognized lines", snap.unrecognized_lines == [])

    summary = to_economy_summary(snap)
    check("credits scaled by /100", summary.credits.value == 102398789)
    check("credits found", summary.credits.found is True)
    check("one station placeholder", len(summary.stations) == 1)
    check("station ware_count is -1 sentinel (not 0)", summary.stations[0].ware_count == -1)
    check(
        "blueprints/sectors/relations marked not_collected",
        summary.fields_not_collected == {"blueprints", "sectors", "faction_relations"},
    )


def test_no_stations_no_false_empty_flag():
    # Regression guard: a live snapshot with zero stations must not somehow
    # produce a StationInfo list that trips the "empty ware inventory" rule.
    lines = ["MONEY 1200000", "END"]
    snap = parse_snapshot_lines(lines)
    summary = to_economy_summary(snap)
    check("no stations", len(summary.stations) == 0)


def test_missing_end_flags_gap():
    lines = ["MONEY 500000", "SHIP ship_s"]
    snap = parse_snapshot_lines(lines)
    check("got_end False when END missing", snap.got_end is False)
    summary = to_economy_summary(snap)
    check(
        "missing-END gap recorded",
        any("END marker" in g for g in summary.data_gaps),
    )


def test_unrecognized_line_captured_not_silently_dropped():
    lines = ["MONEY 500000", "SHIP unknown_class", "GARBAGE", "END"]
    snap = parse_snapshot_lines(lines)
    check("bad ship class captured as unrecognized", "SHIP unknown_class" in snap.unrecognized_lines)
    check("garbage line captured as unrecognized", "GARBAGE" in snap.unrecognized_lines)
    check("bad ship class not counted as a real ship", snap.ship_counts == {})
    summary = to_economy_summary(snap)
    check(
        "unrecognized-line gap recorded",
        any("unrecognized line" in g for g in summary.data_gaps),
    )


def test_no_money_marks_not_found():
    lines = ["SHIP ship_s", "END"]
    snap = parse_snapshot_lines(lines)
    summary = to_economy_summary(snap)
    check("credits not found", summary.credits.found is False)
    check("credits value is None", summary.credits.value is None)


if __name__ == "__main__":
    test_full_snapshot()
    test_no_stations_no_false_empty_flag()
    test_missing_end_flags_gap()
    test_unrecognized_line_captured_not_silently_dropped()
    test_no_money_marks_not_found()
    print("\nAll live-protocol checks passed.")
