import logging
def test_get_domains(test_client,test_domain,login_user):
    response = test_client.get("/api/domains",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 len(data["data"]) == 1
    assert data["data"][0]["name"] == test_domain.name

def test_create_domain(test_client, login_user,login_user_id):
    create_domain ={
                    "id": 0,
                    "tenantid": "1",
                    "name": "abc",
                    "url": "efg",
                    "kintoneuser": "eee",
                    "kintonepwd": "fff",
                    "is_active": True,
                }
    response = test_client.post("/api/domain", json=create_domain,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"]["name"] == create_domain["name"]
    assert data["data"]["url"] == create_domain["url"]
    assert data["data"]["kintoneuser"] == create_domain["kintoneuser"]
    assert data["data"]["is_active"] == create_domain["is_active"]
    assert data["data"]["owner"]["id"] == login_user_id


def test_get_managedomainuser(test_client,test_domain,login_user,login_user_id):
    response = test_client.get("/api/managedomainuser/" + str(test_domain.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"]) == 1
    assert data["data"][0]["id"] == login_user_id


def test_add_delete_userdomain(test_client,test_domain,test_user,login_user,login_user_id):
    userdomain ={
        "userid":test_user["id"],
        "domainid":test_domain.id
    }
    response = test_client.post("/api/userdomain" , json=userdomain,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"]["name"] == test_domain.name
    assert data["data"]["url"] == test_domain.url
    response = test_client.delete(f"/api/domain/{test_domain.id}/{test_user["id"]}" , headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    logging.error(data)
    assert "data" in data
    assert data["data"] is not None
    assert data["data"]["name"] == test_domain.name
    assert data["data"]["url"] == test_domain.url


def test_add_delete_managedomain(test_client,test_domain,test_user,login_user,login_user_id):
    userdomain ={
        "userid":test_user["id"],
        "domainid":test_domain.id
    }
    response = test_client.post("/api/managedomain" , json=userdomain,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"]["name"] == test_domain.name
    assert data["data"]["url"] == test_domain.url
    response = test_client.delete(f"/api/managedomain/{test_domain.id}/{test_user["id"]}" , headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    logging.error(data)
    assert "data" in data
    assert data["data"] is not None
    assert data["data"]["name"] == test_domain.name
    assert data["data"]["url"] == test_domain.url


def test_delete_domain(test_client, login_user,login_user_id):
    delete_domain ={
                    "id": 0,
                    "tenantid": "1",
                    "name": "delete",
                    "url": "delete",
                    "kintoneuser": "delete",
                    "kintonepwd": "delete",
                    "is_active": True,
                }
    response = test_client.post("/api/domain", json=delete_domain,headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    assert response.status_code == 200
    assert "data" in data
    assert data["data"] is not None
    id =  data["data"]["id"]

    response = test_client.delete(f"/api/domain/{id}/{login_user_id}",headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    logging.error(data)

    response = test_client.delete(f"/api/domain/{id}",headers={"Authorization": "Bearer " + login_user})
    data = response.json()
    logging.error(data)
    assert response.status_code == 200
    assert response.json()["data"]["name"] == delete_domain["name"]
    response = test_client.get(f"/api/domain/{id}", headers={"Authorization": "Bearer " + login_user})
    assert response.status_code == 200
    assert "data" not in response.json()


def test_set_defaultuserdomain(test_client, test_domain,login_user):

    response = test_client.put("/api/defaultdomain/"+str(test_domain.id), 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"]["name"] == test_domain.name
    assert data["data"]["url"] == test_domain.url
    assert data["data"]["kintoneuser"] == test_domain.kintoneuser
    assert data["data"]["is_active"] == test_domain.is_active


def test_get_defaultuserdomain(test_client, test_domain,login_user):

    response = test_client.get("/api/defaultdomain", headers={"Authorization": "Bearer " + login_user})
    assert response.status_code == 200
    data = response.json()
    assert "data" in data
    assert data["data"] is not None
    assert data["data"]["name"] == test_domain.name
    assert data["data"]["url"] == test_domain.url
    assert data["data"]["kintoneuser"] == test_domain.kintoneuser
    assert data["data"]["is_active"] == test_domain.is_active


def test_get_domainshareduser(test_client, test_domain,login_user,login_user_id):
    response = test_client.get("/api/domainshareduser/"+str(test_domain.id), 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 len(data["data"]) == 1
    assert data["data"][0]["id"] == login_user_id

def test_edit_domain(test_client, test_domain, login_user):
    update_domain ={
                    "id": test_domain.id,
                    "tenantid": "1",
                    "name": "テスト環境abc",
                    "url": test_domain.url,
                    "kintoneuser": test_domain.kintoneuser,
                    "is_active": True
                }
    response = test_client.put("/api/domain", json=update_domain,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"]["name"] == update_domain["name"]
    assert data["data"]["url"] == update_domain["url"]
    assert data["data"]["kintoneuser"] == update_domain["kintoneuser"]
    assert data["data"]["is_active"] == update_domain["is_active"]