from service_app_test.bases import active_and_confirmed
from service_app_test.test_application.evyos.datas.get_occupants_codes import (
    get_occupants_types,
)

# from service_app_test.test_application.evyos.datas.get_type_codes import get_type_codes_key_and_class


decision_book_items_dict = (
    lambda token, item_comment, info_type_uu_id, unit_price, is_fixed, st, ed: {
        "token": token,
        "item_comment": item_comment,
        "info_type_uu_id": info_type_uu_id,
        "unit_price": unit_price,
        "unit_price_is_fixed": is_fixed,
        "debit_start_date": st,
        "debit_end_date": ed,
        **active_and_confirmed,
    }
)


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


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


def assign_people_to_pre_or_wrt(
    person_uu_id: str, manager_token: str, occupant_code: str, requester
):
    response = requester.post(
        endpoint="/build/decision_book/invitations/assign",
        data={
            "token": manager_token,
            "build_living_space_uu_id": person_uu_id,
            "occupant_type_uu_id": get_occupants_types(
                occupant_code=occupant_code, requester=requester
            )["data"]["uu_id"],
        },
    )
    print("text", response.text)
    print("json", response.json())
    return response.json()


def collect_invitation_to_building_residents(
    attended_dict_list: list, attend_count: int, requester
):
    for cnt, attended_token in enumerate(attended_dict_list):
        attended_dict = {"token": attended_token, "is_attend": False}
        if cnt + 1 < attend_count:
            attended_dict["is_attend"] = True
        response = requester.post(
            endpoint="/build/decision_book/invitations/attend",
            data=attended_dict,
        )
        print("text", response.text)
        print("json", response.json())
    return


def create_decision_book_items_with_occupant_user(
    requester,
    writers_token: str,
    item_comment: str,
    info_type_uu_id: str,
    is_fixed: bool = False,
    unit_price: float = None,
    start_date: str = None,
    end_date: str = None,
):
    print("create_decision_book_items_with_occupant_user : ", writers_token)
    list_of_items = [
        decision_book_items_dict(
            token=writers_token,
            item_comment=item_comment,
            info_type_uu_id=info_type_uu_id,
            unit_price=unit_price,
            is_fixed=is_fixed,
            st=start_date,
            ed=end_date,
        ),
    ]
    for item in list_of_items:
        print("item", item)
        create_decision_book_items(item, requester=requester)
    return


def run_decision_book_items(
    writers_token,
    info_type_uu_id,
    item_comment,
    requester,
    is_fixed=None,
    unit_price=None,
    start_date=None,
    end_date=None,
):
    if start_date and end_date:
        create_decision_book_items_with_occupant_user(
            item_comment=item_comment,
            writers_token=writers_token,
            unit_price=unit_price,
            is_fixed=is_fixed,
            info_type_uu_id=info_type_uu_id,
            start_date=start_date,
            end_date=end_date,
            requester=requester,
        )
    else:
        create_decision_book_items_with_occupant_user(
            writers_token=writers_token,
            item_comment=item_comment,
            unit_price=unit_price,
            is_fixed=is_fixed,
            info_type_uu_id=info_type_uu_id,
            requester=requester,
        )
