import frappe, os, json

def get_manifest():
    app_path = frappe.get_app_path("custom_ui")
    manifest_path = os.path.join(app_path, "custom_ui","public", "dist", ".vite", "manifest.json")
    if not os.path.exists(manifest_path):
        frappe.log_error(message="Manifest file not found for custom_ui", title="Manifest Load Failed")
        return {}
    with open(manifest_path) as f:
        return json.load(f)
    
def create_module():
    print("Creating Custom UI module if it does not exist...")
    
    mod_name = "Custom UI"
    if frappe.db.exists("Module Def", mod_name):
        mod = frappe.get_doc("Module Def", mod_name)
        mod.show_in_sidebar = 1  # make sure it’s visible
        mod.save(ignore_permissions=True)
        print(f"Module '{mod_name}' updated with show_in_sidebar=1")
    else:
        frappe.get_doc({
            "doctype": "Module Def",
            "module_name": mod_name,
            "app_name": "custom_ui",
            "color": "#1976d2",
            "icon": "octicon octicon-code",
            "show_in_sidebar": 1
        }).insert(ignore_permissions=True)
        print(f"Module '{mod_name}' created")

    frappe.db.commit()
    print("Custom UI module creation process completed.")

def on_login_redirect(login_manager):
    frappe.local.response["type"] = "redirect"
    frappe.local.response["location"] = "/app/custom-ui"