# from loggers.loggers import LoginLogger


def get_menu_from_mongo(user_id, company_name):
    from databases.no_sql_models.mongo_database import MongoQuery

    mongo = MongoQuery(
        table_name=company_name.replace(" ", ""), database_name="mongo_database"
    )
    mongo_dict = mongo.parse_json(mongo.get_one(match=user_id, field="user_id")) or {}
    return mongo_dict.get("menu", [])


def load_user_with_erp_details(found_user, access_dict: dict = None):
    duties = []
    employee = found_user.person.employee
    duty_dict = {}
    try:
        duty_dict = employee.duty.get_dict(
            include=["duty_name", "duty_code", "duty_description"]
        )
        duty_dict["buildings"] = []
        duty_dict["response_buildings"] = []
        duty_dict["department"] = employee.duty.department.get_dict(
            include=["department_name", "department_code"]
        )
        duty_dict["company"] = employee.duty.department.company.get_dict(
            include=["formal_name", "public_name", "tax_no", "default_lang_type"]
        )
    except Exception as e:
        # LoginLogger.log_exception(
        #     {
        #         "exc": e,
        #         "user": found_user.uu_id,
        #         "function": "load_user_with_erp_details",
        #     }
        # )
        err = e
        print("MongoQuery load_user_with_erp_details", err)

    for building in list(set(employee.duty.department.company.response_buildings)):
        build_parts = []
        for part in building.parts:
            build_parts.append(
                part.get_dict(
                    include=["uu_id", "part_name", "part_code", "part_description"]
                )
            )
        duty_dict["response_buildings"].append(
            {
                "build": building.get_dict(include=["build_name", "uu_id"]),
                "parts": build_parts,
            }
        )
    for building in list(set(employee.duty.department.company.buildings)):
        build_parts = []
        for part in building.parts:
            build_parts.append(
                part.get_dict(
                    include=[
                        "uu_id",
                        "part_name",
                        "part_code",
                        "part_description",
                    ]
                )
            )
        duty_dict["buildings"].append(
            {
                "build": building.get_dict(include=["build_name", "uu_id"]),
                "parts": build_parts,
            }
        )
    duties.append(duty_dict)
    return_dict = access_dict if access_dict else {}
    return_dict.update(
        {
            "data": {
                "profile": found_user.get_dict(),
                "employee_info": duties,
                "menu": get_menu_from_mongo(
                    found_user.id,
                    company_name=duty_dict.get("company", {}).get("public_name", ""),
                ),
            },
        }
    )
    return return_dict
