import frappe
from frappe.model.document import Document


class KapitalBankSettings(Document):
	def on_update(self):
		self._sync_account_statuses()
		self._sync_customer_statuses()
		self._sync_supplier_statuses()
		self._sync_card_statuses()

	def _sync_account_statuses(self):
		"""Reset Kapital Bank Account status to 'New' when its mapping row is removed
		from account_mappings, or when the row's Bank Account link is cleared."""
		mapped_ibans = {row.iban for row in self.account_mappings if row.iban and row.bank_account}

		orphaned = frappe.get_all(
			"Kapital Bank Account",
			filters={"status": "Mapped"},
			fields=["name"],
		)

		for account in orphaned:
			if account.name not in mapped_ibans:
				frappe.db.set_value(
					"Kapital Bank Account",
					account.name,
					{
						"status": "New",
						"bank_account": None,
					},
					update_modified=False,
				)

	def _sync_customer_statuses(self):
		"""Reset Kapital Bank Customer status to 'New' when its mapping row is removed
		from customer_mappings, or when the row's ERP Customer link is cleared."""
		mapped_names = {row.kb_customer_name for row in self.customer_mappings if row.kb_customer_name and row.erp_customer}

		orphaned = frappe.get_all(
			"Kapital Bank Customer",
			filters={"status": "Mapped"},
			fields=["name", "customer_name"],
		)

		for customer in orphaned:
			if customer.name not in mapped_names:
				frappe.db.set_value(
					"Kapital Bank Customer",
					customer.name,
					{
						"status": "New",
						"mapped_customer": None,
						"customer_group": None,
						"territory": None,
						"payment_terms": None,
					},
					update_modified=False,
				)

	def _sync_supplier_statuses(self):
		"""Reset Kapital Bank Supplier status to 'New' when its mapping row is removed
		from supplier_mappings, or when the row's ERP Supplier link is cleared."""
		mapped_names = {row.kb_supplier_name for row in self.supplier_mappings if row.kb_supplier_name and row.erp_supplier}

		orphaned = frappe.get_all(
			"Kapital Bank Supplier",
			filters={"status": "Mapped"},
			fields=["name", "supplier_name"],
		)

		for supplier in orphaned:
			if supplier.name not in mapped_names:
				frappe.db.set_value(
					"Kapital Bank Supplier",
					supplier.name,
					{
						"status": "New",
						"mapped_supplier": None,
						"supplier_group": None,
						"payment_terms": None,
					},
					update_modified=False,
				)

	def _sync_card_statuses(self):
		"""Reset Kapital Bank Card status to 'New' when its mapping row is removed
		from card_mappings, or when the row's Bank Account link is cleared."""
		mapped_accounts = {row.account_number for row in self.card_mappings if row.account_number and row.bank_account}

		orphaned = frappe.get_all(
			"Kapital Bank Card",
			filters={"status": "Mapped"},
			fields=["name", "account_number"],
		)

		for card in orphaned:
			if card.name not in mapped_accounts:
				frappe.db.set_value(
					"Kapital Bank Card",
					card.name,
					{
						"status": "New",
						"bank_account": None,
					},
					update_modified=False,
				)
