import uuid


def test_users_me(client, admin_tokens, auth_headers):
    response = client.get("/api/v1/users/me", headers=auth_headers(admin_tokens))
    assert response.status_code == 200
    assert response.json()["username"] == "admin"


def test_users_list(client, admin_tokens, auth_headers):
    response = client.get("/api/v1/users/", headers=auth_headers(admin_tokens))
    assert response.status_code == 200
    result = response.json()
    assert "result" in result
    assert isinstance(result["result"], list)


def test_users_org_list(client, admin_tokens, auth_headers):
    response = client.get("/api/v1/users/?load_orgs=true", headers=auth_headers(admin_tokens))
    assert response.status_code == 200
    result = response.json()
    assert "result" in result
    assert isinstance(result["result"], list)
    assert len(result["result"]) > 0
    assert "org_units" in result["result"][0]
    assert len(result["result"][0]["org_units"]) > 0


def test_users_get(client, admin_tokens, auth_headers):

    response = client.get("/api/v1/users/me", headers=auth_headers(admin_tokens))
    assert response.status_code == 200
    user = response.json()
    assert "id" in user
    user_id = user["id"]

    response = client.get(f"/api/v1/users/{user_id}", headers=auth_headers(admin_tokens))
    assert response.status_code == 200


def test_user_get_org_list(client, admin_tokens, auth_headers):
    response = client.get("/api/v1/users/me", headers=auth_headers(admin_tokens))
    assert response.status_code == 200
    user = response.json()
    assert "id" in user
    user_id = user["id"]

    response = client.get(f"/api/v1/users/{user_id}?load_orgs=true", headers=auth_headers(admin_tokens))
    assert response.status_code == 200
    result = response.json()
    
    assert "org_units" in result
    assert len(result["org_units"]) > 0

# Задокуменировано что бы не было переполнения бд, так как юзер удаляется только логически
# def test_users_create_smoke(client, admin_tokens, auth_headers):
#     suffix = uuid.uuid4().hex[:8]
#     username = f"user_{suffix}"
#     # create_payload = {
#     #     "email": f"{username}@example.com",
#     #     "username": username,
#     #     "password": "pass123",
#     #     "full_name": "User One",
#     #     "role_id": 2,
#     # }
#     created = client.put(
#         "/api/v1/users/",
#         json={
#             "email": f"{username}@example.com",
#             "username": username,
#             "password": "pass123",
#             "full_name": "User One",
#             "role_id": 2,
#         },
#         headers=auth_headers(admin_tokens),
#     )
#     assert created.status_code == 200
#     assert created.json()["result"]["username"] == username

#     # Что бы не было переполнения бд
#     deleted = client.delete(
#         f"/api/v1/users/{created.json()['result']['id']}",
#         headers=auth_headers(admin_tokens),
#     )
#     assert deleted.status_code == 200
