"""
Tests for `funkycache.key`.
"""

from dataclasses import dataclass
from pathlib import Path
from typing import Any

from pydantic import BaseModel
import pytest
from _pytest.mark.structures import ParameterSet

from funkycache.key import encode_key_as_json


class Shape(BaseModel):
    """A basic model that uses Pydantic."""

    sides: int
    colour: str


@dataclass
class Greeting:
    """A basic model that uses dataclasses."""

    name: str
    message: str


KEY_ENCODING_TEST_CASES: list[ParameterSet] = [
    pytest.param({"n": "one"}, '{"n":"one"}', id="str"),
    pytest.param({"n": 1}, '{"n":1}', id="int"),
    pytest.param(
        {"n": ["one", "uno", "eins"]}, '{"n":["one","uno","eins"]}', id="list"
    ),
    pytest.param(
        {"n": ("one", "uno", "eins")}, '{"n":["one","uno","eins"]}', id="tuple"
    ),
    pytest.param({"n": []}, '{"n":[]}', id="empty_list"),
    pytest.param({"n": [{"en": "one"}]}, '{"n":[{"en":"one"}]}', id="list_1"),
    pytest.param(
        {
            "n": [
                {"lang": "en", "val": "one"},
                {"lang": "fr", "val": "un"},
                {"lang": "de", "val": "eins"},
            ]
        },
        '{"n":{"_keys":["lang","val"],"_values":[["en","one"],["fr","un"],["de","eins"]]}}',
        id="list_many",
    ),
    pytest.param(
        {"n": Shape(sides=4, colour="red")},
        '{"n":{"colour":"red","sides":4}}',
        id="pydantic",
    ),
    pytest.param(
        {"n": Greeting(name="alex", message="hello")},
        '{"n":{"message":"hello","name":"alex"}}',
        id="dataclass",
    ),
    pytest.param(
        {"long": "a" * 5000, "short": "a"},
        '{"long":"xxh64:b2b67943d4803cd6","short":"a"}',
        id="long_str",
    ),
    pytest.param(
        {"n": {"long": "a" * 5000, "short": "a"}},
        '{"n":{"long":"xxh64:b2b67943d4803cd6","short":"a"}}',
        id="long_str",
    ),
    pytest.param({"n": {Path("1"): "one"}}, '{"n":{"1":"one"}}', id="nested_path_key"),
]


@pytest.mark.parametrize(
    "key, json_key",
    KEY_ENCODING_TEST_CASES,
)
def test_encode_key_as_json(key: dict[str, Any], json_key: str) -> None:
    """
    Tests for encode_key_as_json.
    """
    assert encode_key_as_json(key) == json_key
