from app.core.security import create_access_token, hash_password
from tests.helpers import register_and_verify


def _admin_headers(client) -> dict[str, str]:
    from app.modules.users.repository import get_user_by_email

    admin = get_user_by_email("admin@compton.example")
    token = create_access_token(admin.id, admin.role, admin.is_superuser)
    return {"Authorization": f"Bearer {token}"}


def test_blocked_user_access_token_revoked_after_admin_block(client):
    register_and_verify(client, "blocked-jwt@example.com")
    login = client.post(
        "/api/v1/auth/login",
        json={"email": "blocked-jwt@example.com", "password": "Valid123"},
    )
    assert login.status_code == 200
    access_token = login.json()["access_token"]

    me = client.get("/api/v1/users/me", headers={"Authorization": f"Bearer {access_token}"})
    assert me.status_code == 200

    user_id = me.json()["user"]["id"]
    blocked = client.patch(
        f"/api/v1/admin/users/{user_id}",
        headers=_admin_headers(client),
        json={"status": "blocked"},
    )
    assert blocked.status_code == 200

    revoked = client.get("/api/v1/users/me", headers={"Authorization": f"Bearer {access_token}"})
    assert revoked.status_code == 401
    assert revoked.json()["detail"] == "TOKEN_REVOKED"
