from service_app_test.test_application.evyos.people import (
    create_random_person,
    create_random_user,
    create_random_occupant_user,
)
from service_app_test.test_application.evyos.datas.company_employee_data import (
    departments,
    duties,
    staffs,
)
from service_app_test.bases import active_and_confirmed


print("Program up and running **-------------------------------------")


def create_department_via_list(list_of_departments, requester):
    for department in list_of_departments:
        request_department = requester.post(
            data={
                **department,
                **active_and_confirmed,
            },
            endpoint="department/create",
        )
        print(request_department.json())


def create_duty_via_list(list_of_duties, requester):
    for duty in list_of_duties:
        duty_dict = {
            "duty_name": duty["duty_name"],
            "duty_code": duty["duty_code"],
            "duty_description": duty["duty_description"],
            **active_and_confirmed,
        }
        request_duty = requester.post(data=duty_dict, endpoint="duty/create")
        print(request_duty.json())


def create_duties_via_list(list_of_duties, company_uu_id, requester):
    for duty in list_of_duties:
        department_response = requester.post(
            endpoint="department/list",
            data={"query": dict(department_name=duty["department_name"])},
        )
        selected_department = department_response.json()["data"][0]["uu_id"]

        duty_response = requester.post(
            endpoint="duty/list",
            data={"query": dict(duty_name=duty["duty_name"])},
        )
        selected_duties = duty_response.json()["data"][0]["uu_id"]
        duty_dict = {
            "department_uu_id": selected_department,
            "duties_uu_id": selected_duties,
            **active_and_confirmed,
        }
        request_duty = requester.post(data=duty_dict, endpoint="duties/create")
        print(request_duty.json())

    requester.post(
        endpoint="authentication/select", data={"company_uu_id": company_uu_id}
    )


def create_staff_via_list(list_of_staff, requester):
    for staff in list_of_staff:
        duty_response = requester.post(
            endpoint="duty/list",
            data={"query": dict(duty_name=staff["duty"])},
        )
        selected_duty = duty_response.json()["data"][0]["uu_id"]
        duties_response = requester.post(
            endpoint="duties/get_by_duty_uuid",
            data={"duty_uu_id": selected_duty},
        )
        selected_duties = duties_response.json()["data"][0]["uu_id"]
        staff_dict = staff
        del staff_dict["duty"]
        staff_dict["duties_uu_id"] = selected_duties
        request_staff = requester.post(
            data={**staff_dict, **active_and_confirmed}, endpoint="staff/create"
        )
        print(request_staff.json())


def create_employee_via_list(list_of_staff, requester):
    for staff in list_of_staff:
        duty_response = requester.post(
            endpoint="duty/list",
            data={"query": dict(duty_name=staff["duty_name"])},
        )
        selected_duty = duty_response.json()["data"][0]["uu_id"]
        duties_response = requester.post(
            endpoint="duties/get_by_duty_uuid",
            data={"duty_uu_id": selected_duty},
        )
        selected_duties = duties_response.json()["data"][0]["uu_id"]
        request_staff = requester.post(
            data={"duties_uu_id": selected_duties}, endpoint="staff/get_by_duties_uu_id"
        )
        selected_staffs_obj = request_staff.json()["data"][0]
        selected_staffs_uu_id = selected_staffs_obj["uu_id"]
        if "Temsilcisi" in selected_staffs_obj["staff_name"]:
            for i in range(0, 5):
                request_create_staff = requester.post(
                    endpoint="employee/create",
                    data={
                        "staff_uu_id": selected_staffs_uu_id,
                        "people_uu_id": None,
                        **active_and_confirmed,
                    },
                )
                print(request_create_staff.json())
        elif "Elemanı" in selected_staffs_obj["staff_name"]:
            for i in range(0, 2):
                request_create_staff = requester.post(
                    endpoint="employee/create",
                    data={
                        "staff_uu_id": selected_staffs_uu_id,
                        "people_uu_id": None,
                        **active_and_confirmed,
                    },
                )
                print(request_create_staff.json())
        else:
            request_create_staff = requester.post(
                endpoint="employee/create",
                data={
                    "staff_uu_id": selected_staffs_uu_id,
                    "people_uu_id": None,
                    **active_and_confirmed,
                },
            )
            print(request_create_staff.json())

        print(request_staff.json())


def create_people_via_list(people_count: int, requester):
    for people in range(people_count):
        people = create_random_person()
        people_dict = people.model_dump()
        request_people = requester.post(
            data={**people_dict, **active_and_confirmed}, endpoint="people/create"
        )
        response_person = request_people.json()
        selected_person_uu_id = response_person["data"]["uu_id"]
        insert_user = create_random_user(selected_person_uu_id)
        request_user = requester.post(
            data={**active_and_confirmed, **insert_user}, endpoint="user/create"
        )
        response_user = request_user.json()
        selected_user_uu_id = response_user["data"]["uu_id"]


def create_occupant_people_via_list(people_count: int, requester):
    for people in range(people_count):
        people = create_random_person()
        people_dict = people.model_dump()
        request_people = requester.post(
            data={**people_dict, **active_and_confirmed}, endpoint="people/create"
        )
        response_person = request_people.json()
        selected_person_uu_id = response_person["data"]["uu_id"]
        insert_user = create_random_occupant_user(selected_person_uu_id)
        request_user = requester.post(
            data={**active_and_confirmed, **insert_user}, endpoint="user/create"
        )
        response_user = request_user.json()
        selected_user_uu_id = response_user["data"]["uu_id"]


def run_company_and_depends(company_uu_id, requester):
    create_department_via_list(list_of_departments=departments, requester=requester)
    create_duty_via_list(list_of_duties=duties, requester=requester)
    create_duties_via_list(
        list_of_duties=duties, company_uu_id=company_uu_id, requester=requester
    )
    create_staff_via_list(list_of_staff=staffs, requester=requester)
    create_employee_via_list(list_of_staff=duties, requester=requester)
    create_people_via_list(people_count=10, requester=requester)
    create_occupant_people_via_list(people_count=70, requester=requester)


print("Program ended **-------------------------------------")
