
import logging
def test_users_list(test_client,login_user):

    response = test_client.get("/api/v1/users", headers={"Authorization": "Bearer " + login_user})
    assert response.status_code == 200
    data = response.json()
    assert len(data["data"]) == 2 

def test_users_list_for_admin(test_client,login_admin):
    
    response = test_client.get("/api/v1/users", headers={"Authorization": "Bearer " + login_admin})
    assert response.status_code == 200
    data = response.json()
    assert "data" in data
    assert len(data["data"]) == 3 

def test_user_create(test_client,login_user):
    user_data = {
        "email": "newuser1@example.com",
        "password": "password123",
        "first_name": "New",
        "last_name": "User",
        "is_active": True,
        "is_superuser": False
    }
    response = test_client.post("/api/v1/users", json=user_data, headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    logging.error(data)
    assert response.status_code == 200
    assert "data" in data
    assert data["data"] is not None
    assert data["data"]["id"] > 0
    assert data["data"]["email"] == user_data["email"] 
    assert data["data"]["first_name"] == user_data["first_name"] 
    assert data["data"]["last_name"] == user_data["last_name"] 
    assert data["data"]["is_active"] == user_data["is_active"] 
    assert data["data"]["is_superuser"] == user_data["is_superuser"] 
   
def test_admin_create(test_client,login_user):
    user_data = {
        "email": "newuser2@example.com",
        "password": "password123",
        "first_name": "New",
        "last_name": "User",
        "is_active": True,
        "is_superuser": True
    }
    response = test_client.post("/api/v1/users", json=user_data, headers={"Authorization": "Bearer " + login_user})
    assert response.status_code == 200
    data = response.json()
    assert "data" not in data

def test_admin_create_for_admin(test_client,login_admin):
    user_data = {
        "email": "admin@example.com",
        "password": "password123",
        "first_name": "New",
        "last_name": "User",
        "is_active": True,
        "is_superuser": True
    }
    response = test_client.post("/api/v1/users", json=user_data, headers={"Authorization": "Bearer " + login_admin})
    data = response.json()
    logging.error(data)
    assert response.status_code == 200
    assert "data" in data
    assert data["data"] is not None
    assert data["data"]["id"] > 0
    assert data["data"]["email"] == user_data["email"] 
    assert data["data"]["first_name"] == user_data["first_name"] 
    assert data["data"]["last_name"] == user_data["last_name"] 
    assert data["data"]["is_active"] == user_data["is_active"] 
    assert data["data"]["is_superuser"] == user_data["is_superuser"] 

def test_user_details(test_client,login_user_id, login_user,user):
    id = login_user_id
    response = test_client.get("/api/v1/users/"+ str(id), headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    logging.error(data)
    assert response.status_code == 200
    assert data["data"]["email"] == user["email"]
    assert data["data"]["first_name"] == user["first_name"]
    assert data["data"]["last_name"] == user["last_name"]
    assert data["data"]["is_active"] == user["is_active"]
    assert data["data"]["is_superuser"] == user["is_superuser"]
    assert data["data"]["id"] == id

def test_user_edit(test_client, login_user_id,login_user,user):
    id = login_user_id
    user_data = {
        "email": user["email"],
        "first_name": "Updated",
        "last_name": "test",
        "is_active": True,
        "is_superuser": False
    }
    response = test_client.put("/api/v1/users/" + str(id), json=user_data, headers={"Authorization": "Bearer " + login_user})

    assert response.status_code == 200
    data = response.json()
    assert data["data"]["email"] == user["email"]
    assert data["data"]["first_name"] == user_data["first_name"] 
    assert data["data"]["last_name"] == user_data["last_name"] 
    assert data["data"]["is_active"] == user["is_active"]
    assert data["data"]["id"] == id

def test_user_delete(test_client, login_user):
    user_data = {
        "email": "delete@example.com",
        "password": "password123",
        "first_name": "delete",
        "last_name": "User",
        "is_active": True,
        "is_superuser": False
    }
    response = test_client.post("/api/v1/users", json=user_data, headers={"Authorization": "Bearer " + login_user})
    assert response.status_code == 200
    data = response.json()
    id =  data["data"]["id"]
    response = test_client.delete("/api/v1/users/"+ str(id),headers={"Authorization": "Bearer " + login_user})
    assert response.status_code == 200
    assert response.json()["data"]["email"] == "delete@example.com"
    response = test_client.get("/api/v1/users/"+ str(id), headers={"Authorization": "Bearer " + login_user})
    assert response.status_code == 200
    assert "data" not in response.json()

def test_role_assign(test_client, login_user_id,login_user,test_role):
    userroles ={
        "userid":login_user_id,
        "roleids":[test_role["id"]]
    }
    response = test_client.post("/api/v1/userrole", json=userroles, headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    logging.error(data)
    assert response.status_code == 200
    response = test_client.get("/api/v1/users/"+ str(login_user_id), headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    logging.error(data)
    assert response.status_code == 200
    assert "data" in data
    assert len(data["data"]["roles"]) == 1

def test_roles_get(test_client,login_user):
    response = test_client.get("/api/v1/roles", headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    logging.error(data)
    assert response.status_code == 200
    assert len(data["data"]) == 0 

def test_roles_admin_get(test_client,login_admin):
    response = test_client.get("/api/v1/roles", headers={"Authorization": "Bearer " + login_admin})
    data = response.json()
    logging.error(data)
    assert response.status_code == 200
    assert len(data["data"]) == 1