"""
通知公告测试用例
"""

import pytest
import time
from api.notice_api import SysNoticeAPI, SysMessageAPI


@pytest.mark.notice
@pytest.mark.regression
class TestSysNotice:
    """系统公告测试类"""

    @pytest.mark.asyncio
    async def test_create_notice_success(self, authenticated_client):
        """测试创建公告成功"""
        api = SysNoticeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "noticeTitle": f"测试公告_{timestamp}",
            "noticeType": "1",
            "noticeContent": "这是测试公告内容",
            "status": "0"
        }

        response = await api.create(data)

        assert response.status_code in [200, 201]
        result = response.json()
        assert result["noticeTitle"] == data["noticeTitle"]

    @pytest.mark.asyncio
    async def test_get_all_notices(self, authenticated_client):
        """测试获取所有公告"""
        api = SysNoticeAPI(authenticated_client)

        response = await api.get_all()

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

    @pytest.mark.asyncio
    async def test_get_notice_by_id(self, authenticated_client):
        """测试根据ID获取公告"""
        api = SysNoticeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "noticeTitle": f"测试公告_{timestamp}",
            "noticeType": "1",
            "noticeContent": "测试内容",
            "status": "0"
        }
        create_response = await api.create(data)
        notice_id = create_response.json()["id"]

        response = await api.get_by_id(notice_id)

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

    @pytest.mark.asyncio
    async def test_get_notice_by_status(self, authenticated_client):
        """测试根据状态获取公告"""
        api = SysNoticeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "noticeTitle": f"测试公告_{timestamp}",
            "noticeType": "1",
            "noticeContent": "测试内容",
            "status": "0"
        }
        await api.create(data)

        response = await api.get_by_status("0")

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

    @pytest.mark.asyncio
    async def test_update_notice(self, authenticated_client):
        """测试更新公告"""
        api = SysNoticeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "noticeTitle": f"测试公告_{timestamp}",
            "noticeType": "1",
            "noticeContent": "原始内容",
            "status": "0"
        }
        create_response = await api.create(data)
        notice_id = create_response.json()["id"]

        update_data = {
            "noticeTitle": f"更新后_{timestamp}",
            "noticeType": "1",
            "noticeContent": "更新后内容",
            "status": "0"
        }
        response = await api.update(notice_id, update_data)

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

    @pytest.mark.asyncio
    async def test_delete_notice(self, authenticated_client):
        """测试删除公告"""
        api = SysNoticeAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "noticeTitle": f"测试公告_{timestamp}",
            "noticeType": "1",
            "noticeContent": "测试内容",
            "status": "0"
        }
        create_response = await api.create(data)
        notice_id = create_response.json()["id"]

        response = await api.delete(notice_id)

        assert response.status_code in [200, 204]


@pytest.mark.notice
@pytest.mark.regression
class TestSysMessage:
    """用户消息测试类"""

    @pytest.mark.asyncio
    async def test_create_message(self, authenticated_client):
        """测试创建消息"""
        api = SysMessageAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "userId": 1,
            "title": f"测试消息_{timestamp}",
            "content": "这是测试消息内容",
            "type": "1"
        }

        response = await api.create(data)

        assert response.status_code in [200, 201]
        result = response.json()
        assert result["title"] == data["title"]

    @pytest.mark.asyncio
    async def test_get_messages_by_user(self, authenticated_client):
        """测试获取用户消息"""
        api = SysMessageAPI(authenticated_client)
        user_id = 1

        response = await api.get_by_user(user_id)

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

    @pytest.mark.asyncio
    async def test_get_unread_count(self, authenticated_client):
        """测试获取未读消息数量"""
        api = SysMessageAPI(authenticated_client)
        user_id = 1

        response = await api.get_unread_count(user_id)

        assert response.status_code == 200

    @pytest.mark.asyncio
    async def test_mark_message_as_read(self, authenticated_client):
        """测试标记消息为已读"""
        api = SysMessageAPI(authenticated_client)
        timestamp = int(time.time() * 1000)
        data = {
            "userId": 1,
            "title": f"测试消息_{timestamp}",
            "content": "测试内容",
            "type": "1"
        }
        create_response = await api.create(data)
        message_id = create_response.json()["id"]

        response = await api.mark_as_read(message_id)

        assert response.status_code == 200
