import logging
def test_create_flow(test_client,test_domain,test_app_id,login_user):
    test_flow={
        "flowid": "73e82bee-76a2-4347-a069-e21bf5e21111",
        "appid": test_app_id,
        "appname": "test_app",
        "eventid": "a",
        "name": "保存をクリックしたとき",
        "content": ""
    }
    response = test_client.post("/api/flow", json=test_flow,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"]["domainurl"] == test_domain.url
    assert data["data"]["flowid"] == test_flow["flowid"] 
    assert data["data"]["appid"] == test_flow["appid"] 
    assert data["data"]["eventid"] == test_flow["eventid"] 
    assert data["data"]["content"] == test_flow["content"] 

def test_delete_flow(test_client,test_domain,test_app_id,login_user):
    response = test_client.delete("/api/flow/73e82bee-76a2-4347-a069-e21bf5e21111",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
 
def test_edit_flow(test_client,test_domain,test_app_id,login_user):
    test_flow={
        "flowid": "73e82bee-76a2-4347-a069-e21bf5e21111",
        "appid": test_app_id,
        "appname": "test_app_new",
        "eventid": "abc",
        "name": "保存をクリックしたとき",
        "content": ""
    }
    response = test_client.put("/api/flow", json=test_flow,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"]["domainurl"] == test_domain.url
    assert data["data"]["flowid"] == test_flow["flowid"] 
    assert data["data"]["appid"] == test_flow["appid"] 
    assert data["data"]["eventid"] == test_flow["eventid"] 
    assert data["data"]["content"] == test_flow["content"] 

def test_appversions_update(test_client,test_domain,test_app_id,login_user):
    app_version ={
        "versionname": "version1",
        "comment": "save version1",
        "appid": test_app_id
    }
    response = test_client.post("/api/apps", json=app_version,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"]["domainurl"] == test_domain.url
    assert data["data"]["version"] == 1
    assert data["data"]["appid"] == app_version["appid"] 
    assert data["data"]["versionname"] == app_version["versionname"] 
    assert data["data"]["is_saved"] == False

def test_apps_list(test_client,login_user):
    response = test_client.get("/api/apps", headers={"Authorization": "Bearer " + login_user})
    assert response.status_code == 200
    data = response.json()
    assert "data" in data
    assert data["data"] is not None
    assert len(data["data"]) == 1


def test_appversions_list(test_client,test_domain,test_app_id,login_user):
    response = test_client.get("/api/appversions/" + test_app_id , headers={"Authorization": "Bearer " + login_user})
    assert response.status_code == 200
    data = response.json()
    assert "data" in data
    assert data["data"] is not None
    assert len(data["data"]) == 1
    assert "versionname" in data["data"][0]

def test_appversions_change(test_client,test_domain,test_app_id,login_user):
    app_version ={
        "versionname": "version2",
        "comment": "test",
        "appid": test_app_id
    }
    response = test_client.post("/api/apps", json=app_version,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"]["version"] == 2
    assert data["data"]["versionname"] == app_version["versionname"]
    assert data["data"]["is_saved"] == False


    response = test_client.put("/api/appversions/" + test_app_id +"/1", 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"]["domainurl"] == test_domain.url 
    assert data["data"]["version"] == 1
    assert data["data"]["appid"] == test_app_id 


def test_delete_app(test_client,test_app_id,login_user):
    response = test_client.delete("/api/apps/"+ test_app_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