import frappe


ITEM_GROUP_RENAMES = {
    "Products": "Məhsullar",
    "Raw Material": "Xammal",
    "Services": "Xidmətlər",
    "Sub Assemblies": "Alt məclislər",
    "Consumable": "İstehlak materialı",
}

SUPPLIER_GROUP_RENAMES = {
    "Local": "Yerli",
    "Electrical": "Elektrik",
    "Hardware": "Avadanlıq",
    "Pharmaceutical": "Əczaçılıq",
    "Distributor": "Distribyutor",
}


def rename_default_groups(args=None):
    for old_name, new_name in ITEM_GROUP_RENAMES.items():
        _rename_doc("Item Group", old_name, new_name)

    for old_name, new_name in SUPPLIER_GROUP_RENAMES.items():
        _rename_doc("Supplier Group", old_name, new_name)


def _rename_doc(doctype, old_name, new_name):
    if frappe.db.exists(doctype, new_name):
        return

    if not frappe.db.exists(doctype, old_name):
        return

    try:
        frappe.rename_doc(doctype, old_name, new_name, force=True)
        frappe.db.commit()
        print(f"[jey_erp] Renamed {doctype}: {old_name} -> {new_name}")
    except Exception as e:
        print(f"[jey_erp] Error renaming {doctype} '{old_name}': {e}")
