"""Tests for .godotiq.json configuration."""

from __future__ import annotations

import json
import pytest

from godotiq.config import GodotIQConfig, load_config


class TestConfigLoading:
    def test_load_missing_file(self, tmp_path):
        """Missing config returns defaults."""
        config = load_config(str(tmp_path / ".godotiq.json"))
        assert config.disabled_tools == []
        assert config.get_default_detail() == "normal"

    def test_load_valid_config(self, tmp_path):
        config_data = {
            "disabled_tools": ["godotiq_screenshot"],
            "protected_files": ["*.gd"],
            "server": {"default_detail": "brief"},
        }
        p = tmp_path / ".godotiq.json"
        p.write_text(json.dumps(config_data))
        config = load_config(str(p))
        assert "godotiq_screenshot" in config.disabled_tools
        assert config.get_default_detail() == "brief"

    def test_invalid_json_returns_defaults(self, tmp_path):
        p = tmp_path / ".godotiq.json"
        p.write_text("{invalid json")
        config = load_config(str(p))
        assert config.disabled_tools == []
        assert config.get_default_detail() == "normal"


class TestToolDisabled:
    def test_tool_disabled(self, tmp_path):
        config_data = {"disabled_tools": ["godotiq_exec", "godotiq_input"]}
        p = tmp_path / ".godotiq.json"
        p.write_text(json.dumps(config_data))
        config = load_config(str(p))
        assert config.is_tool_disabled("godotiq_exec") is True
        assert config.is_tool_disabled("godotiq_scene_map") is False

    def test_empty_disabled_list(self):
        config = GodotIQConfig()
        assert config.is_tool_disabled("godotiq_exec") is False


class TestFileProtection:
    def test_default_protected_files(self):
        config = GodotIQConfig()
        assert config.is_file_protected("project.godot") is True
        assert config.is_file_protected("scripts/manager.gd") is False

    def test_glob_pattern(self):
        config = GodotIQConfig()
        assert config.is_file_protected("saves/save1.tres") is True
        assert config.is_file_protected("icon.png.import") is True

    def test_custom_protected_files(self, tmp_path):
        config_data = {"protected_files": ["*.gd", "assets/**"]}
        p = tmp_path / ".godotiq.json"
        p.write_text(json.dumps(config_data))
        config = load_config(str(p))
        assert config.is_file_protected("scripts/main.gd") is True
        assert config.is_file_protected("scenes/main.tscn") is False


class TestAssetOrigins:
    def test_get_asset_scale(self, tmp_path):
        config_data = {
            "asset_origins": {
                "meshy": {"default_scale": [0.3, 0.3, 0.3]},
            },
        }
        p = tmp_path / ".godotiq.json"
        p.write_text(json.dumps(config_data))
        config = load_config(str(p))
        assert config.get_asset_scale("meshy") == [0.3, 0.3, 0.3]
        assert config.get_asset_scale("unknown") is None

    def test_no_origins(self):
        config = GodotIQConfig()
        assert config.get_asset_scale("kenney") is None


class TestServerConfig:
    def test_default_server(self):
        config = GodotIQConfig()
        assert config.server["max_nodes_per_response"] == 200
        assert config.server["screenshot_default_scale"] == 0.25

    def test_custom_server(self, tmp_path):
        config_data = {"server": {"default_detail": "full", "max_nodes_per_response": 100}}
        p = tmp_path / ".godotiq.json"
        p.write_text(json.dumps(config_data))
        config = load_config(str(p))
        assert config.get_default_detail() == "full"
        assert config.server["max_nodes_per_response"] == 100
