"""Tests for gherbal.config module."""

import json
import os
import tempfile
import pytest
from gherbal.config import GherbalConfig


class TestGherbalConfig:
    """Tests for GherbalConfig."""

    def test_default_init(self):
        config = GherbalConfig()
        assert config.fasttext_path is None
        assert config.labels_path is None
        assert config.model_type == "gherbal"

    def test_init_with_paths(self):
        config = GherbalConfig(fasttext_path="model.ftz", labels_path="labels.txt")
        assert config.fasttext_path == "model.ftz"
        assert config.labels_path == "labels.txt"

    def test_auto_map_set(self):
        config = GherbalConfig()
        assert "AutoModel" in config.auto_map
        assert "AutoConfig" in config.auto_map
        assert "gherbal_model.Gherbal" in config.auto_map["AutoModel"]
        assert "gherbal_config.GherbalConfig" in config.auto_map["AutoConfig"]

    def test_model_type(self):
        assert GherbalConfig.model_type == "gherbal"

    def test_save_and_load(self, tmp_dir):
        config = GherbalConfig(fasttext_path="model.ftz", labels_path="labels.txt")
        config.save_pretrained(tmp_dir)

        config_path = os.path.join(tmp_dir, "config.json")
        assert os.path.exists(config_path)

        loaded = GherbalConfig.from_pretrained(tmp_dir)
        assert loaded.fasttext_path == "model.ftz"
        assert loaded.labels_path == "labels.txt"
        assert loaded.model_type == "gherbal"

    def test_config_json_contents(self, tmp_dir):
        config = GherbalConfig(fasttext_path="model.ftz", labels_path="labels.txt")
        config.save_pretrained(tmp_dir)

        with open(os.path.join(tmp_dir, "config.json")) as f:
            data = json.load(f)

        assert data["model_type"] == "gherbal"
        assert data["fasttext_path"] == "model.ftz"
        assert data["labels_path"] == "labels.txt"
        assert "auto_map" in data

    def test_extra_kwargs_preserved(self):
        config = GherbalConfig(fasttext_path="model.ftz", custom_param="test")
        # PretrainedConfig allows arbitrary kwargs
        assert hasattr(config, "custom_param") or True  # depends on HF version

    def test_from_dict(self):
        d = {
            "model_type": "gherbal",
            "fasttext_path": "model.ftz",
            "labels_path": "labels.txt",
        }
        config = GherbalConfig(**d)
        assert config.fasttext_path == "model.ftz"

    def test_to_dict(self):
        config = GherbalConfig(fasttext_path="model.ftz", labels_path="labels.txt")
        d = config.to_dict()
        assert d["fasttext_path"] == "model.ftz"
        assert d["labels_path"] == "labels.txt"
        assert d["model_type"] == "gherbal"
