import sys
import tempfile
import unittest
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent

sys.path.insert(0, str(PROJECT_ROOT))

import core.io.cfg_format as manager


class TestCSSModValveCfg(unittest.TestCase):
    def setUp(self) -> None:
        self.sandbox = tempfile.TemporaryDirectory()
        self.sandbox_path = Path(self.sandbox.name)

    def tearDown(self) -> None:
        self.sandbox.cleanup()

    def test_parse_cfg_file_data_types(self) -> None:
        """Verify that the Regex parser correctly extracts the CVARs and types the values."""
        cfg_file = Path(self.sandbox_path) / "test.cfg"
        cfg_content = (
            "// Commentaire A\n"
            'sm_test_bool "1"\n\n'
            "// Commentaire B\n"
            'sm_test_float "12.5"\n'
            'sm_test_str "hello_world"'
        )
        cfg_file.write_text(cfg_content, encoding="utf-8")

        # Exécution directe du parseur
        cvars = manager.parse_cfg_file(cfg_file)

        self.assertEqual(len(cvars), 3)

        cvar_bool = next(c for c in cvars if c["cvar"] == "sm_test_bool")
        self.assertEqual(cvar_bool["type"], "boolean")
        self.assertEqual(cvar_bool["default"], True)
        self.assertEqual(cvar_bool["description"], "Commentaire A")

        cvar_float = next(c for c in cvars if c["cvar"] == "sm_test_float")
        self.assertEqual(cvar_float["type"], "number")
        self.assertEqual(cvar_float["default"], 12.5)

        cvar_str = next(c for c in cvars if c["cvar"] == "sm_test_str")
        self.assertEqual(cvar_str["type"], "string")
        self.assertEqual(cvar_str["default"], "hello_world")
