from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from spectra_replay_normalize import normalize_spectra_replay, strip_model_prefix


def test_expands_v3_entity_delta_columns_to_legacy_path() -> None:
    replay = {
        "entities": [
            {
                "uid": "1",
                "unit": "tankModels/ussr_kv_220",
                "t": [1000, 250, 250],
                "x": [1600, 16, -32],
                "y": [64, 0, 16],
                "z": [-3200, 160, 0],
            }
        ]
    }

    normalized = normalize_spectra_replay(replay)

    entity = normalized["entities"][0]
    assert entity["unit"] == "ussr_kv_220"
    assert entity["model_path"] == "tankModels/ussr_kv_220"
    assert entity["path"] == [
        [1000, 100.0, 4.0, -200.0],
        [1250, 101.0, 4.0, -190.0],
        [1500, 99.0, 5.0, -190.0],
    ]


def test_expands_v3_ticket_delta_columns_to_legacy_rows() -> None:
    replay = {"tickets": {"1": [[0, 1000, 500], [16000, -20, -30]]}}

    normalized = normalize_spectra_replay(replay)

    assert normalized["tickets"]["1"] == [
        [0, 16000],
        [1000, 15980],
        [1500, 15950],
    ]


def test_strips_event_unit_model_prefixes_but_preserves_model_path() -> None:
    replay = {
        "events": {
            "kills": [
                {
                    "offender_unit": "airModels/spitfire_ix_usa",
                    "offended_unit": "tankModels/ussr_kv_220",
                    "offender_pos": [1600, 320, -800],
                    "offended_pos": [3200, 64, -1600],
                }
            ]
        },
        "players": {
            "1": {"units": [{"unit": "tankModels/ussr_kv_220"}]},
        },
    }

    normalized = normalize_spectra_replay(replay)
    kill = normalized["events"]["kills"][0]

    assert strip_model_prefix("tankModels/ussr_kv_220") == "ussr_kv_220"
    assert kill["offender_unit"] == "spitfire_ix_usa"
    assert kill["offender_unit_model_path"] == "airModels/spitfire_ix_usa"
    assert kill["offended_unit"] == "ussr_kv_220"
    assert kill["offended_unit_model_path"] == "tankModels/ussr_kv_220"
    assert kill["offender_pos"] == [100.0, 20.0, -50.0]
    assert kill["offended_pos"] == [200.0, 4.0, -100.0]
    assert normalized["players"]["1"]["units"][0]["unit"] == "ussr_kv_220"
