import frappe

def before_uninstall():
    start_erpnext_scheduled_send_email_job()

def start_erpnext_scheduled_send_email_job():
    # Get list of Scheduled Job Type docs with that method
    job_docs = frappe.get_all(
        "Scheduled Job Type",
        filters={"method": "erpnext.crm.doctype.email_campaign.email_campaign.send_email_to_leads_or_contacts"},
        limit_page_length=1
    )

    if not job_docs:
        frappe.msgprint("Scheduled Job Type not found.")
        return

    job_name = job_docs[0].name
    job_doc = frappe.get_doc("Scheduled Job Type", job_name)

    # Set stopped = 1
    job_doc.stopped = 0
    job_doc.save(ignore_permissions=True)
    frappe.db.commit()

    frappe.msgprint(f"Scheduled Job Type '{job_doc.method}' stopped successfully.")
