import arrow

from service_app_test.bases import active_and_confirmed
from service_app_test.test_application.evyos.datas.company_employee_data import (
    list_options,
)
from service_app_test.test_application.evyos.datas.get_type_codes import (
    get_type_codes_key_and_class,
)
from service_app_test.test_application.evyos.address_building import list_building


decision_book_dict = lambda build_uu_id, resp_company_uu_id: {
    "build_uu_id": build_uu_id,
    "decision_type": "RBM",
    "meeting_date": str(arrow.now().date()),
    "is_out_sourced": True,
    "resp_company_uu_id": resp_company_uu_id,
    **active_and_confirmed,
}
event_bind_to_occupant = lambda event_uu_id_list, build_living_space_uu_id: {
    "event_uu_id_list": list(event_uu_id_list),
    "build_living_space_uu_id": str(build_living_space_uu_id),
}


def get_uu_id_of_enum(enum_class: str, key: str, requester):
    get_type_codes_response = get_type_codes_key_and_class(
        class_name=enum_class, key_name=key, requester=requester
    )
    return get_type_codes_response["data"]["uu_id"]


def list_company(requester):
    response = requester.post(
        endpoint="/company/list",
        data=list_options,
    )
    print("text", response.text)
    print("json", response.json())
    return response.json()


def list_decision_books(requester):
    response = requester.post(
        endpoint="/build/decision_book/list",
        data=list_options,
    )
    print("text", response.text)
    print("json", response.json())
    return response.json()


def list_decision_book_items(requester):
    response = requester.post(
        endpoint="/build/decision_book/items/list",
        data=list_options,
    )
    print("text", response.text)
    print("json", response.json())
    return response.json()


def create_decision_book(decision_book, requester):
    response = requester.post(
        endpoint="/build/decision_book/create",
        data=decision_book,
    )
    print("text", response.text)
    print("json", response.json())
    return response.json()


def give_service_access_to_occupant(occupant_dict, requester):
    response = requester.post(
        endpoint="/bind/services/occupant",
        data=occupant_dict,
    )
    print("text", response.text)
    print("json", response.json())
    return response.json()


def give_event_access_to_build_decision_book_items(occupant_dict, requester):
    response = requester.post(
        endpoint="/bind/events/occupant",
        data={
            "occupant_uu_id": occupant_dict["occupant_uu_id"],
            "build_part_uu_id": occupant_dict["build_part_uu_id"],
            "event_uu_id_list": list(occupant_dict["event_uu_id_list"]),
            "is_confirmed": True,
        },
    )
    print("text", response.text)
    print("json", response.json())
    return response.json()


def run_decision_book_depends(requester):
    build_response = list_building(requester=requester)
    selected_build_uu_id = build_response["data"][0]["uu_id"]

    company_response = list_company(requester=requester)
    selected_company_uu_id = company_response["data"][0]["uu_id"]

    create_decision_book(
        decision_book_dict(
            build_uu_id=selected_build_uu_id,
            resp_company_uu_id=selected_company_uu_id,
        ),
        requester=requester,
    )
    decision_book = list_decision_books(requester=requester)["data"][0]["uu_id"]
    return decision_book
