"""
字典管理测试用例
"""

import pytest
import time
from api.dict_api import DictTypeAPI, DictDataAPI


@pytest.mark.dict
@pytest.mark.regression
class TestDictType:
    """字典类型测试类"""

    @pytest.mark.asyncio
    async def test_create_dict_type_success(self, authenticated_client):
        """测试创建字典类型成功"""
        api = DictTypeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "dictName": f"测试字典_{timestamp}",
            "dictType": f"test_{timestamp}",
            "status": "0"
        }

        response = await api.create(data)

        assert response.status_code == 201
        result = response.json()
        assert result["dictName"] == data["dictName"]
        assert result["dictType"] == data["dictType"]

    @pytest.mark.asyncio
    async def test_get_all_dict_types(self, authenticated_client):
        """测试获取所有字典类型"""
        api = DictTypeAPI(authenticated_client)

        response = await api.get_all()

        assert response.status_code == 200
        assert isinstance(response.json(), list)

    @pytest.mark.asyncio
    async def test_get_dict_type_by_id(self, authenticated_client):
        """测试根据ID获取字典类型"""
        api = DictTypeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        create_data = {
            "dictName": f"测试字典_{timestamp}",
            "dictType": f"test_{timestamp}",
            "status": "0"
        }
        create_response = await api.create(create_data)
        dict_id = create_response.json()["id"]

        response = await api.get_by_id(dict_id)

        assert response.status_code == 200
        assert response.json()["id"] == dict_id

    @pytest.mark.asyncio
    async def test_update_dict_type(self, authenticated_client):
        """测试更新字典类型"""
        api = DictTypeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        create_data = {
            "dictName": f"测试字典_{timestamp}",
            "dictType": f"test_{timestamp}",
            "status": "0"
        }
        create_response = await api.create(create_data)
        dict_id = create_response.json()["id"]

        update_data = {
            "dictName": f"更新后_{timestamp}",
            "dictType": f"test_{timestamp}",
            "status": "0"
        }
        response = await api.update(dict_id, update_data)

        assert response.status_code == 200
        assert response.json()["dictName"] == f"更新后_{timestamp}"

    @pytest.mark.asyncio
    async def test_delete_dict_type(self, authenticated_client):
        """测试删除字典类型"""
        api = DictTypeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        create_data = {
            "dictName": f"测试字典_{timestamp}",
            "dictType": f"test_{timestamp}",
            "status": "0"
        }
        create_response = await api.create(create_data)
        dict_id = create_response.json()["id"]

        response = await api.delete(dict_id)

        assert response.status_code == 204


@pytest.mark.dict
@pytest.mark.regression
class TestDictData:
    """字典数据测试类"""

    @pytest.mark.asyncio
    async def test_create_dict_data_success(self, authenticated_client):
        """测试创建字典数据成功"""
        dict_type_api = DictTypeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        dict_type_data = {
            "dictName": f"测试字典类型_{timestamp}",
            "dictType": f"test_type_{timestamp}",
            "status": "0"
        }
        dict_type_response = await dict_type_api.create(dict_type_data)
        dict_type_id = dict_type_response.json()["id"]

        dict_data_api = DictDataAPI(authenticated_client)
        data = {
            "dictSort": 1,
            "dictLabel": f"测试标签_{timestamp}",
            "dictValue": f"test_value_{timestamp}",
            "dictType": f"test_type_{timestamp}",
            "status": "0"
        }

        response = await dict_data_api.create(data)

        assert response.status_code == 201
        result = response.json()
        assert result["dictLabel"] == data["dictLabel"]
        assert result["dictValue"] == data["dictValue"]

    @pytest.mark.asyncio
    async def test_get_dict_data_by_type(self, authenticated_client):
        """测试根据类型获取字典数据"""
        dict_type_api = DictTypeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        dict_type = f"test_type_{timestamp}"
        dict_type_data = {
            "dictName": f"测试字典类型_{timestamp}",
            "dictType": dict_type,
            "status": "0"
        }
        await dict_type_api.create(dict_type_data)

        dict_data_api = DictDataAPI(authenticated_client)
        data = {
            "dictSort": 1,
            "dictLabel": f"测试标签_{timestamp}",
            "dictValue": f"test_value_{timestamp}",
            "dictType": dict_type,
            "status": "0"
        }
        await dict_data_api.create(data)

        response = await dict_data_api.get_by_type(dict_type)

        assert response.status_code == 200
        result = response.json()
        assert len(result) > 0
        assert result[0]["dictType"] == dict_type
