"""Remove the legacy "ƏDV tutulan və tutulmayan əməliyyatlar" group and its
children. Their content was split into two prefixed groups
("ƏDV tutulan dövriyyə barədə məlumat" / "ƏDV tutulan əməliyyat sayılmayan
dövriyyə barədə məlumat") which ship via the tax_article fixture.

Runs in post_model_sync, i.e. before fixtures are imported on migrate, so the
new prefixed articles are created cleanly without colliding with the old ones.
Idempotent: a no-op once the legacy records are gone.
"""

import frappe

OLD_GROUP = "ƏDV tutulan və tutulmayan əməliyyatlar"


def execute():
	if not frappe.db.exists("DocType", "Tax Article"):
		return

	# children first (NestedSet refuses to drop a group that still has children)
	for name in frappe.get_all(
		"Tax Article", filters={"parent_tax_article": OLD_GROUP}, pluck="name"
	):
		frappe.delete_doc("Tax Article", name, force=1, ignore_permissions=True)

	if frappe.db.exists("Tax Article", OLD_GROUP):
		frappe.delete_doc("Tax Article", OLD_GROUP, force=1, ignore_permissions=True)
