import frappe


def execute():
    """Permanently drop the Won/Lost checkbox Custom Fields from Sales Invoice."""

    fieldnames = ["won", "lost"]

    for fieldname in fieldnames:
        name = frappe.db.get_value(
            "Custom Field", {"dt": "Sales Invoice", "fieldname": fieldname}, "name"
        )
        if not name:
            continue
        try:
            frappe.delete_doc("Custom Field", name, ignore_permissions=True, force=True)
            print(f"Deleted Custom Field: Sales Invoice.{fieldname}")
        except Exception as e:
            print(f"Failed to delete Sales Invoice.{fieldname}: {e}")

    # Drop any Property Setters that targeted these fields.
    frappe.db.delete(
        "Property Setter",
        {"doc_type": "Sales Invoice", "field_name": ("in", fieldnames)},
    )

    frappe.clear_cache(doctype="Sales Invoice")
    frappe.db.commit()
