from databases import (
    Modules,
    Duty,
    Services,
    OccupantTypes,
)


def create_endpoints_from_api_functions(routers):
    from application.create_file import create_app
    from databases import EndpointRestriction

    api_app = create_app(routers=routers)

    for route in api_app.routes:
        route_path, route_summary = (
            str(getattr(route, "path")),
            str(getattr(route, "name")) or "",
        )

        methods = [method.lower() for method in getattr(route, "methods")]
        for route_method in methods:
            restriction = EndpointRestriction.find_or_create(
                **dict(
                    endpoint_method=route_method,
                    endpoint_name=route_path,
                    endpoint_desc=route_summary.replace("_", " "),
                    endpoint_function=route_summary,
                )
            )
            restriction.endpoint_code = f"AR{str(restriction.id).zfill(3)}"
            restriction.save_and_confirm()

    EndpointRestriction.save()
    return api_app


def create_services_building(module_dict: dict):
    """
    4. Service [Bina]           Yönetim             - OPTIONAL
    5. Service [Bina]           Yasal İşler         - OPTIONAL
    6. Service [Bina]           Temizlik            - OPTIONAL
    7. Service [Bina]           Isınma Sistemi      - OPTIONAL
    8. Service [Bina]           Güvenlik Sistemi    + OPTIONAL
    9. Service [Bina]           Bakım               + OPTIONAL
    10. Service [Bina]          Onarım              + OPTIONAL
    11 Service [Bina]           Gözlem              + OPTIONAL
    """
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Building Management",
        service_description="Building Management Service",
        service_code="SR-BLD-MNG",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Building Legal Affairs",
        service_description="Building Legal Affairs Service",
        service_code="SR-BLD-LGL",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Building Cleaning",
        service_description="Building Cleaning Service",
        service_code="SR-BLD-CLN",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Building Heating System",
        service_description="Building Heating System Service",
        service_code="SR-BLD-HTS",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Building Security System",
        service_description="Building Security System Service",
        service_code="SR-BLD-SEC",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Building Maintenance",
        service_description="Building Maintenance Service",
        service_code="SR-BLD-MNT",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Building Repair",
        service_description="Building Repair Service",
        service_code="SR-BLD-RPR",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Building Observation",
        service_description="Building Observation Service",
        service_code="SR-BLD-OBS",
    )
    created_service.save_and_confirm()
    return


def create_services_flat(module_dict: dict):
    """
    11. Service [Daire]         Kiralama           + OPTIONAL
    12. Service [Daire]         Satış              + OPTIONAL
    13. Service [Daire]         Tadiilat           + OPTIONAL
    14. Service [Daire]         Temizlik           + OPTIONAL
    11 Service [Daire]           Gözlem              + OPTIONAL
    """
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Flat Rent",
        service_description="Flat Rent Service",
        service_code="SR-FLT-RNT",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Flat Sale",
        service_description="Flat Sale Service",
        service_code="SR-FLT-SAL",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Flat Renovation",
        service_description="Flat Renovation Service",
        service_code="SR-FLT-RNV",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Flat Cleaning",
        service_description="Flat Cleaning Service",
        service_code="SR-FLT-CLN",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Flat Observation",
        service_description="Flat Observation Service",
        service_code="SR-FLT-OBS",
    )
    created_service.save_and_confirm()
    return


def create_services_authenticate(module_dict: dict):
    authentication_service = Services.find_or_create(
        **module_dict,
        service_name="Authenticate",
        service_description="Authenticate Service",
        service_code="AUTH",
    )
    authentication_service.save_and_confirm()
    return


def create_services_meeting(module_dict: dict):

    created_service = Services.find_or_create(
        **module_dict,
        service_name="Meeting Regular",
        service_description="Regular Meeting Service",
        service_code="MEET-REG",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Meeting Emergency",
        service_description="Emergency Meeting Service",
        service_code="MEET-EMR",
    )
    created_service.save_and_confirm()
    created_service = Services.find_or_create(
        **module_dict,
        service_name="Meeting Demand",
        service_description="Demand Meeting Service",
        service_code="MEET-DMN",
    )
    created_service.save_and_confirm()
    return


def create_modules_and_services_and_actions():

    erp_module = Modules.find_or_create(
        **{
            "module_name": "EVYOS ERP",
            "module_description": "EVYOS Enterprise Resource Planning",
            "module_code": "EVYOS-ERP",
            "module_layer": 1,
            "is_default_module": False,
        }
    )
    erp_module.save_and_confirm()

    build_module = Modules.find_or_create(
        **{
            "module_name": "Bina Yönetim Modülü",
            "module_description": "Building Management Module",
            "module_code": "BLD-MNG",
            "module_layer": 1,
            "is_default_module": False,
        }
    )
    build_module.save_and_confirm()

    user_module = Modules.find_or_create(
        **{
            "module_name": "Kullancı Modülü",
            "module_description": "Kullanıcı Genel Modülü",
            "module_code": "USR-PUB",
            "module_layer": 1,
            "is_default_module": True,
        }
    )
    user_module.save_and_confirm()

    erp_module_module_dict = dict(
        module_id=erp_module.id,
        module_uu_id=str(erp_module.uu_id),
    )
    build_module_module_dict = dict(
        module_id=build_module.id,
        module_uu_id=str(build_module.uu_id),
    )
    user_module_module_dict = dict(
        module_id=user_module.id,
        module_uu_id=str(user_module.uu_id),
    )

    duty_objects = Duty.filter_all(
        Duty.duty_code.notin_(["BULK", "OCCUPANT", "BM0001"])
    )
    for duty_object in duty_objects.data:
        created_service = Services.find_or_create(
            **erp_module_module_dict,
            service_name=duty_object.duty_name,
            service_description=duty_object.duty_description,
            service_code=f"SRE-{duty_object.duty_code}",
            related_responsibility=duty_object.duty_code,
        )
        created_service.save_and_confirm()

    occupant_types = OccupantTypes.filter_all()
    for occupant_type in occupant_types.data:
        created_service = Services.find_or_create(
            **build_module_module_dict,
            service_name=occupant_type.occupant_type,
            service_description=occupant_type.occupant_description,
            service_code=f"SRO-{occupant_type.occupant_code}",
            related_responsibility=occupant_type.occupant_code,
        )
        created_service.save_and_confirm()

    create_services_authenticate(module_dict=user_module_module_dict)
    create_services_meeting(module_dict=build_module_module_dict)
    create_services_building(module_dict=build_module_module_dict)
    create_services_flat(module_dict=build_module_module_dict)
    return
    #
    # super_admin_module_created=None
    # create_address_service(add_module=super_admin_module_created)
    # create_post_code_service(add_module=super_admin_module_created)
    # create_authentication_service(add_module=super_admin_module_created)
    # create_build_service(add_module=super_admin_module_created)
    # create_build_parts_service(add_module=super_admin_module_created)
    # create_build_area_service(add_module=super_admin_module_created)
    # create_build_sites_service(add_module=super_admin_module_created)
    # # create_build_types_service(add_module=super_admin_module_created)
    # create_living_spaces_service(add_module=super_admin_module_created)
    # create_company_service(add_module=super_admin_module_created)
    # create_department_service(add_module=super_admin_module_created)
    # create_duties_service(add_module=super_admin_module_created)
    # create_duty_service(add_module=super_admin_module_created)
    # create_employee_service(add_module=super_admin_module_created)
    # create_staff_service(add_module=super_admin_module_created)
    #
    # create_decision_book_service(add_module=super_admin_module_created)
    # create_decision_book_items_service(add_module=super_admin_module_created)
    # create_build_decision_book_items_debits_service(
    #     add_module=super_admin_module_created
    # )
    # # create_build_decision_book_person_service(add_module=super_admin_module_created)
    # # create_build_decision_book_person_service(add_module=super_admin_module_created)
    #
    # create_actions_service(add_module=super_admin_module_created)
    # create_events_service(add_module=super_admin_module_created)
    # create_model_service(add_module=super_admin_module_created)
    # create_model_entities_service(add_module=super_admin_module_created)
    # create_modules_service(add_module=super_admin_module_created)
    # create_services_service(add_module=super_admin_module_created)
    # create_event_to_bind_people_service(add_module=super_admin_module_created)
    # create_bind_service_service(add_module=super_admin_module_created)
    #
    # create_people_service(add_module=super_admin_module_created)
    # create_project_decision_book_service(add_module=super_admin_module_created)
    # create_project_decision_book_items_service(add_module=super_admin_module_created)
    # create_build_project_decision_book_items_debits_service(
    #     add_module=super_admin_module_created
    # )
    # create_build_project_decision_book_person_service(
    #     add_module=super_admin_module_created
    # )
    # create_endpoint_restriction_service(add_module=super_admin_module_created)
    # create_users_service(add_module=super_admin_module_created)
    #
    # return

    # account_service_endpoint = EndpointRestriction.find_one(
    #     endpoint_function="account_service"
    # )
    # account_service = Services.find_or_create(
    #     service_name="Account",
    #     service_description="Account Service",
    #     service_code="ACC",
    # )
    # address_service = Services.find_or_create(
    #     service_name="Address",
    #     service_description="Address Service",
    #     service_code="ADD",
    # )
    # api_service = Services.find_or_create(
    #     service_name="Api",
    #     service_description="Api Service",
    #     service_code="API",
    # )
    # application_service = Services.find_or_create(
    #     service_name="Application",
    #     service_description="Application Service",
    #     service_code="APP",
    # )
