"""Tests for web AppState."""

from pathlib import Path

import pytest

from impakt.web.state import AppState

MME_DATA = Path(__file__).parent.parent / "mme_data"
FIXTURE_DATA = Path(__file__).parent.parent / "fixtures" / "sample_mme"


class TestAppState:
    def test_initially_empty(self):
        state = AppState()
        assert state.is_empty
        assert state.total_channels == 0
        assert state.primary_test is None

    def test_load_test(self):
        state = AppState()
        loaded = state.load_test(FIXTURE_DATA)
        assert not state.is_empty
        assert loaded.test_id == "IMPAKT_SYNTH_001"
        assert loaded.channel_count == 26
        assert state.primary_test is loaded

    def test_load_multiple_tests(self):
        state = AppState()
        t1 = state.load_test(FIXTURE_DATA)

        if (MME_DATA / "VW1FGS15").exists():
            t2 = state.load_test(MME_DATA / "VW1FGS15")
            assert len(state.tests) == 2
            assert state.primary_test is t1  # First loaded is primary
            assert state.total_channels == 26 + 10

    def test_remove_test(self):
        state = AppState()
        loaded = state.load_test(FIXTURE_DATA)
        state.remove_test(loaded.test_id)
        assert state.is_empty

    def test_get_channel(self):
        state = AppState()
        state.load_test(FIXTURE_DATA)
        ch = state.get_channel("IMPAKT_SYNTH_001", "11HEAD0000ACXA")
        assert ch is not None
        assert ch.name == "11HEAD0000ACXA"

    def test_get_channel_missing(self):
        state = AppState()
        state.load_test(FIXTURE_DATA)
        ch = state.get_channel("IMPAKT_SYNTH_001", "NONEXISTENT")
        assert ch is None

    def test_resolve_channel_with_key(self):
        state = AppState()
        state.load_test(FIXTURE_DATA)
        ch = state.resolve_channel("IMPAKT_SYNTH_001::11HEAD0000ACXA")
        assert ch is not None

    def test_resolve_channel_primary_default(self):
        state = AppState()
        state.load_test(FIXTURE_DATA)
        ch = state.resolve_channel("11HEAD0000ACXA")
        assert ch is not None

    def test_resolve_channel_with_cfc(self):
        state = AppState()
        state.load_test(FIXTURE_DATA)
        ch = state.resolve_channel("11HEAD0000ACXA", cfc_class=600)
        assert ch is not None
        assert ch.cfc_class == 600

    def test_flat_channel_list(self):
        state = AppState()
        state.load_test(FIXTURE_DATA)
        items = state.flat_channel_list()
        assert len(items) == 26
        assert all("value" in item and "label" in item for item in items)

    def test_build_channel_tree(self):
        state = AppState()
        state.load_test(FIXTURE_DATA)
        tree = state.build_channel_tree()
        assert "IMPAKT_SYNTH_001" in tree
        # Should have hierarchical structure
        test_tree = tree["IMPAKT_SYNTH_001"]
        assert len(test_tree) > 0


@pytest.mark.skipif(not (MME_DATA / "3239").exists(), reason="Real MME data not available")
class TestAppStateRealData:
    def test_load_real_mme(self):
        state = AppState()
        loaded = state.load_test(MME_DATA / "3239")
        assert loaded.test_id == "3239"
        assert loaded.channel_count == 133

    def test_channel_tree_real_data(self):
        state = AppState()
        state.load_test(MME_DATA / "3239")
        tree = state.build_channel_tree()
        assert "3239" in tree
        # Should contain "Driver" in some key
        test_tree = tree["3239"]
        assert any("Driver" in k for k in test_tree)
