"""Tests for the Mock OA endpoints (token generation, login, user listing)."""

import pytest
from httpx import AsyncClient


@pytest.mark.asyncio
class TestMockToken:
    """POST /api/v1/mock/token — generate test JWT."""

    async def test_generate_token_default(self, client: AsyncClient, api_prefix: str):
        resp = await client.post(f"{api_prefix}/mock/token", json={
            "user_id": "test_user",
        })
        assert resp.status_code == 200
        body = resp.json()
        assert "access_token" in body
        assert body["token_type"] == "bearer"
        assert len(body["access_token"]) > 20

    async def test_generate_token_with_claims(self, client: AsyncClient, api_prefix: str):
        resp = await client.post(f"{api_prefix}/mock/token", json={
            "user_id": "user_001",
            "office_id": "O_17",
            "dept_id": "D_05",
            "area_id": "A_01",
            "role_ids": ["R_03"],
        })
        assert resp.status_code == 200
        body = resp.json()
        assert "access_token" in body


@pytest.mark.asyncio
class TestMockLogin:
    """POST /api/v1/mock/login — predefined test users."""

    async def test_login_admin(self, client: AsyncClient, api_prefix: str):
        resp = await client.post(f"{api_prefix}/mock/login", json={
            "username": "admin",
            "password": "admin123",
        })
        assert resp.status_code == 200
        body = resp.json()
        assert body["user_id"] == "user_admin"
        assert body["role"] == "admin"
        assert "access_token" in body

    async def test_login_regular_user(self, client: AsyncClient, api_prefix: str):
        resp = await client.post(f"{api_prefix}/mock/login", json={
            "username": "zhang_san",
            "password": "user123",
        })
        assert resp.status_code == 200
        body = resp.json()
        assert body["user_id"] == "user_001"
        assert body["display_name"] == "张三（财务科）"

    async def test_login_wrong_password(self, client: AsyncClient, api_prefix: str):
        resp = await client.post(f"{api_prefix}/mock/login", json={
            "username": "admin",
            "password": "wrong_password",
        })
        assert resp.status_code == 401

    async def test_login_unknown_user(self, client: AsyncClient, api_prefix: str):
        resp = await client.post(f"{api_prefix}/mock/login", json={
            "username": "nonexistent",
            "password": "any",
        })
        assert resp.status_code == 401


@pytest.mark.asyncio
class TestMockUsers:
    """GET /api/v1/mock/users — list test users."""

    async def test_list_users(self, client: AsyncClient, api_prefix: str):
        resp = await client.get(f"{api_prefix}/mock/users")
        assert resp.status_code == 200
        body = resp.json()
        assert isinstance(body, list)
        assert len(body) >= 4
        usernames = [u["username"] for u in body]
        assert "admin" in usernames
        assert "zhang_san" in usernames
        assert "li_si" in usernames
        assert "wang_wu" in usernames
