import xhiveframework


def execute():
	if "healthcare" in xhiveframework.get_installed_apps():
		return

	xhiveframework.delete_doc("Workspace", "Healthcare", ignore_missing=True, force=True)

	pages = xhiveframework.get_all("Page", {"module": "healthcare"}, pluck="name")
	for page in pages:
		xhiveframework.delete_doc("Page", page, ignore_missing=True, force=True)

	reports = xhiveframework.get_all("Report", {"module": "healthcare", "is_standard": "Yes"}, pluck="name")
	for report in reports:
		xhiveframework.delete_doc("Report", report, ignore_missing=True, force=True)

	print_formats = xhiveframework.get_all(
		"Print Format", {"module": "healthcare", "standard": "Yes"}, pluck="name"
	)
	for print_format in print_formats:
		xhiveframework.delete_doc("Print Format", print_format, ignore_missing=True, force=True)

	xhiveframework.reload_doc("website", "doctype", "website_settings")
	forms = xhiveframework.get_all("Web Form", {"module": "healthcare", "is_standard": 1}, pluck="name")
	for form in forms:
		xhiveframework.delete_doc("Web Form", form, ignore_missing=True, force=True)

	dashboards = xhiveframework.get_all("Dashboard", {"module": "healthcare", "is_standard": 1}, pluck="name")
	for dashboard in dashboards:
		xhiveframework.delete_doc("Dashboard", dashboard, ignore_missing=True, force=True)

	dashboards = xhiveframework.get_all(
		"Dashboard Chart", {"module": "healthcare", "is_standard": 1}, pluck="name"
	)
	for dashboard in dashboards:
		xhiveframework.delete_doc("Dashboard Chart", dashboard, ignore_missing=True, force=True)

	xhiveframework.reload_doc("desk", "doctype", "number_card")
	cards = xhiveframework.get_all("Number Card", {"module": "healthcare", "is_standard": 1}, pluck="name")
	for card in cards:
		xhiveframework.delete_doc("Number Card", card, ignore_missing=True, force=True)

	titles = ["Lab Test", "Prescription", "Patient Appointment"]
	items = xhiveframework.get_all("Portal Menu Item", filters=[["title", "in", titles]], pluck="name")
	for item in items:
		xhiveframework.delete_doc("Portal Menu Item", item, ignore_missing=True, force=True)

	doctypes = xhiveframework.get_all("DocType", {"module": "healthcare", "custom": 0}, pluck="name")
	for doctype in doctypes:
		xhiveframework.delete_doc("DocType", doctype, ignore_missing=True)

	xhiveframework.delete_doc("Module Def", "Healthcare", ignore_missing=True, force=True)

	custom_fields = {
		"Sales Invoice": ["patient", "patient_name", "ref_practitioner"],
		"Sales Invoice Item": ["reference_dt", "reference_dn"],
		"Stock Entry": ["inpatient_medication_entry"],
		"Stock Entry Detail": ["patient", "inpatient_medication_entry_child"],
	}
	for doc, fields in custom_fields.items():
		filters = {"dt": doc, "fieldname": ["in", fields]}
		records = xhiveframework.get_all("Custom Field", filters=filters, pluck="name")
		for record in records:
			xhiveframework.delete_doc("Custom Field", record, ignore_missing=True, force=True)
