# Copyright (c) 2026, JeyERP and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document


class BankIntegrationCustomer(Document):
	def autoname(self):
		# Name the record after the customer name itself (like Kapital Bank
		# Customer), truncated to 140 chars, with a -N suffix on collision
		# (collisions are possible since multiple Bank Integrations can hold
		# the same counterparty).
		if not self.customer_name:
			return
		name = str(self.customer_name)
		if len(name) > 140:
			name = name[:137] + "..."
		original = name
		counter = 1
		while frappe.db.exists("Bank Integration Customer", name):
			suffix = f"-{counter}"
			name = original[: 140 - len(suffix)] + suffix
			counter += 1
		self.name = name
