import pytest
from httpx import AsyncClient


@pytest.mark.asyncio
@pytest.mark.unit
async def test_login_success(client: AsyncClient, admin_user):
    """测试用户登录成功"""
    response = await client.post(
        "/api/v1/auth/login",
        json={"username": "admin", "password": "admin123"},
    )

    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True
    assert data["message"] == "登录成功"
    assert "token" in data["data"]
    assert data["data"]["user"]["username"] == "admin"
    assert data["data"]["user"]["role"] == "admin"


@pytest.mark.asyncio
@pytest.mark.unit
async def test_login_wrong_username(client: AsyncClient, admin_user):
    """测试错误的用户名"""
    response = await client.post(
        "/api/v1/auth/login",
        json={"username": "wrong", "password": "admin123"},
    )

    assert response.status_code == 401
    data = response.json()
    assert data["success"] is False
    assert data["error_code"] == "1002"


@pytest.mark.asyncio
@pytest.mark.unit
async def test_login_wrong_password(client: AsyncClient, admin_user):
    """测试错误的密码"""
    response = await client.post(
        "/api/v1/auth/login",
        json={"username": "admin", "password": "wrong"},
    )

    assert response.status_code == 401
    data = response.json()
    assert data["success"] is False
    assert data["error_code"] == "1002"


@pytest.mark.asyncio
@pytest.mark.unit
async def test_login_missing_username(client: AsyncClient, admin_user):
    """测试缺少用户名"""
    response = await client.post(
        "/api/v1/auth/login",
        json={"password": "admin123"},
    )

    assert response.status_code == 400
    data = response.json()
    assert data["success"] is False
    assert data["error_code"] == "1001"


@pytest.mark.asyncio
@pytest.mark.unit
async def test_login_missing_password(client: AsyncClient, admin_user):
    """测试缺少密码"""
    response = await client.post(
        "/api/v1/auth/login",
        json={"username": "admin"},
    )

    assert response.status_code == 400
    data = response.json()
    assert data["success"] is False
    assert data["error_code"] == "1001"


@pytest.mark.asyncio
@pytest.mark.unit
async def test_login_inactive_user(client: AsyncClient, inactive_user):
    """测试未激活用户登录"""
    response = await client.post(
        "/api/v1/auth/login",
        json={"username": "inactive", "password": "inactive123"},
    )

    assert response.status_code == 401
    data = response.json()
    assert data["success"] is False


@pytest.mark.asyncio
@pytest.mark.unit
async def test_get_current_user_success(client: AsyncClient, admin_token: str):
    """测试获取当前用户信息成功"""
    response = await client.get(
        "/api/v1/auth/me",
        headers={"Authorization": f"Bearer {admin_token}"},
    )

    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True
    assert data["data"]["username"] == "admin"
    assert data["data"]["role"] == "admin"


@pytest.mark.asyncio
@pytest.mark.unit
async def test_get_current_user_no_token(client: AsyncClient):
    """测试未提供token获取用户信息"""
    response = await client.get("/api/v1/auth/me")

    assert response.status_code == 401
    data = response.json()
    assert data["success"] is False


@pytest.mark.asyncio
@pytest.mark.unit
async def test_get_current_user_invalid_token(client: AsyncClient):
    """测试无效token获取用户信息"""
    response = await client.get(
        "/api/v1/auth/me",
        headers={"Authorization": "Bearer invalid_token"},
    )

    assert response.status_code == 401
    data = response.json()
    assert data["success"] is False
    assert data["error_code"] == "1003"


@pytest.mark.asyncio
@pytest.mark.unit
async def test_logout_success(client: AsyncClient, admin_token: str):
    """测试登出成功"""
    response = await client.post(
        "/api/v1/auth/logout",
        headers={"Authorization": f"Bearer {admin_token}"},
    )

    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True
    assert data["message"] == "登出成功"
