import frappe
from frappe.model.document import Document


class KapitalBankPurpose(Document):
	def autoname(self):
		if not self.purpose_keyword:
			return

		name = str(self.purpose_keyword)

		if len(name) > 140:
			name = name[:137] + "..."

		counter = 1
		original_name = name
		while frappe.db.exists("Kapital Bank Purpose", name):
			suffix = f"-{counter}"
			max_length = 140 - len(suffix)
			name = original_name[:max_length] + suffix
			counter += 1

		self.name = name
