"""
Tests for codec layer (JSON/YAML encoding/decoding).
"""

import pytest
import json

from kilit.codecs import JSONCodec, CodecRegistry


class TestJSONCodec:
    """Test JSON codec."""

    def test_encode_simple_dict(self):
        """Encode simple dictionary."""
        codec = JSONCodec()
        data = {"key": "value"}
        
        encoded = codec.dumps(data)
        
        assert isinstance(encoded, str)
        assert json.loads(encoded) == data

    def test_encode_nested_dict(self):
        """Encode nested dictionary."""
        codec = JSONCodec()
        data = {
            "database": {
                "host": "localhost",
                "port": 5432,
                "credentials": {
                    "username": "admin",
                    "password": "secret"
                }
            }
        }
        
        encoded = codec.dumps(data)
        assert json.loads(encoded) == data

    def test_decode_simple_json(self):
        """Decode simple JSON string."""
        codec = JSONCodec()
        json_str = '{"key": "value"}'
        
        decoded = codec.loads(json_str)
        
        assert decoded == {"key": "value"}

    def test_decode_nested_json(self):
        """Decode nested JSON string."""
        codec = JSONCodec()
        json_str = '{"database": {"host": "localhost", "port": 5432}}'
        
        decoded = codec.loads(json_str)
        
        assert decoded == {"database": {"host": "localhost", "port": 5432}}

    def test_roundtrip(self):
        """Encode and decode roundtrip."""
        codec = JSONCodec()
        data = {"key": "value", "nested": {"inner": "data"}}
        
        encoded = codec.dumps(data)
        decoded = codec.loads(encoded)
        
        assert decoded == data


class TestCodecRegistry:
    """Test codec registry and detection."""

    def test_detect_json_from_json_ext(self):
        """Detect JSON codec from .json extension."""
        registry = CodecRegistry()
        codec = registry.get_codec_for_path("config/creds.json")
        assert isinstance(codec, JSONCodec)

    def test_detect_json_from_json_enc_ext(self):
        """Detect JSON codec from .json.enc extension."""
        registry = CodecRegistry()
        codec = registry.get_codec_for_path("config/creds.json.enc")
        assert isinstance(codec, JSONCodec)

    def test_detect_yaml_from_yaml_ext(self):
        """Detect YAML codec from .yaml extension."""
        registry = CodecRegistry()
        try:
            from kilit.codecs import YAMLCodec
            codec = registry.get_codec_for_path("config/creds.yaml")
            assert isinstance(codec, YAMLCodec)
        except ImportError:
            pytest.skip("PyYAML not installed")

    def test_detect_yaml_from_yml_ext(self):
        """Detect YAML codec from .yml extension."""
        registry = CodecRegistry()
        try:
            from kilit.codecs import YAMLCodec
            codec = registry.get_codec_for_path("config/creds.yml")
            assert isinstance(codec, YAMLCodec)
        except ImportError:
            pytest.skip("PyYAML not installed")

    def test_detect_yaml_from_yaml_enc_ext(self):
        """Detect YAML codec from .yaml.enc extension."""
        registry = CodecRegistry()
        try:
            from kilit.codecs import YAMLCodec
            codec = registry.get_codec_for_path("config/creds.yaml.enc")
            assert isinstance(codec, YAMLCodec)
        except ImportError:
            pytest.skip("PyYAML not installed")

    def test_get_codec_by_name_json(self):
        """Get JSON codec by name."""
        registry = CodecRegistry()
        codec = registry.get_codec_by_name("json")
        assert isinstance(codec, JSONCodec)

    def test_list_codecs(self):
        """List available codecs."""
        registry = CodecRegistry()
        codecs = registry.list_codecs()
        assert "json" in codecs
