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

def create_modules_and_services_and_actions(db_session):

    Duty.set_session(db_session)
    Services.set_session(db_session)
    Modules.set_session(db_session)
    OccupantTypes.set_session(db_session)

    erp_module = Modules.query.filter_by(module_code = "EVYOS-ERP").first()
    if not erp_module:
        erp_module = Modules.create(
            **{
                "module_name": "EVYOS ERP", "module_description": "EVYOS Enterprise Resource Planning", "module_code": "EVYOS-ERP",
                "module_layer": 1, "is_default_module": False, "is_confirmed": True,
            }
        )
        erp_module.save()

    build_module = Modules.query.filter_by(module_code = "BLD-MNG").first()
    if not build_module:
        build_module = Modules.create(
            **{
                "module_name": "Bina Yönetim Modülü", "module_description": "Building Management Module", "module_code": "BLD-MNG",
                "module_layer": 1, "is_default_module": False, "is_confirmed": True,
            }
        )
        build_module.save()

    user_module = Modules.query.filter_by(module_code = "USR-PUB").first()
    if not user_module:
        user_module = Modules.create(
            **{
                "module_name": "Kullancı Modülü", "module_description": "Kullanıcı Genel Modülü", "module_code": "USR-PUB", "module_layer": 1, 
                "is_default_module": True, "is_confirmed": True
            }
        )
        user_module.save()

    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))
    duty_objects = Duty.query.filter(Duty.module_id == erp_module.id).all()
    if not duty_objects:
        raise Exception("Duty objects not found")

    for duty_object in duty_objects:
        created_service = Services.query.filter(Services.service_code == f"SRE-{duty_object.duty_code}").first()
        if not created_service:
            created_service = Services.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,
                is_confirmed=True,
            )
            created_service.save()

    occupant_types = OccupantTypes.query.filter(OccupantTypes.module_id == build_module.id).all()
    if not occupant_types:
        raise Exception("Occupant types not found")
    for occupant_type in occupant_types:
        created_service = Services.query.filter(Services.service_code == f"SRO-{occupant_type.occupant_code}").first()
        if not created_service:
            created_service = Services.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,
                is_confirmed=True,
            )
            created_service.save()
