"""Tests for core.services.scan_service.get_maps_for_mod."""

import sys
import tempfile
import unittest
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from core import constants
from core.io.umc_writer import write
from core.services.scan_service import get_maps_for_mod


class TestGetMapsForMod(unittest.TestCase):
    def setUp(self) -> None:
        self.sandbox = tempfile.TemporaryDirectory()
        self.sandbox_path = Path(self.sandbox.name)
        constants.UMC_MASTER_FILE = self.sandbox_path / "umc_mapcycle_master.txt"

    def tearDown(self) -> None:
        self.sandbox.cleanup()

    def test_returns_empty_dict_when_master_file_missing(self) -> None:
        self.assertEqual(get_maps_for_mod("any_mod"), {})

    def test_groups_maps_by_category(self) -> None:
        write(
            constants.UMC_MASTER_FILE,
            {
                "Normal": {
                    "options": {},
                    "maps": {
                        "de_a": {"target_mods": "my_mod"},
                        "de_b": {"target_mods": "other_mod"},
                    },
                },
                "Aim": {
                    "options": {},
                    "maps": {"aim_c": {"target_mods": "my_mod"}},
                },
            },
            create_backup=False,
        )

        result = get_maps_for_mod("my_mod")

        self.assertEqual(result, {"Normal": ["de_a"], "Aim": ["aim_c"]})

    def test_returns_empty_dict_when_mod_has_no_maps(self) -> None:
        write(
            constants.UMC_MASTER_FILE,
            {"Normal": {"options": {}, "maps": {"de_a": {"target_mods": "other_mod"}}}},
            create_backup=False,
        )

        self.assertEqual(get_maps_for_mod("my_mod"), {})

    def test_ignores_maps_without_target_mods(self) -> None:
        write(
            constants.UMC_MASTER_FILE,
            {"Normal": {"options": {}, "maps": {"de_free": {}}}},
            create_backup=False,
        )

        self.assertEqual(get_maps_for_mod("my_mod"), {})


if __name__ == "__main__":
    unittest.main()
