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

import frappe
from frappe.model.document import Document


class BankIntegrationSupplier(Document):
	def autoname(self):
		# Name the record after the supplier name itself (like Kapital Bank
		# Supplier), truncated to 140 chars, with a -N suffix on collision.
		if not self.supplier_name:
			return
		name = str(self.supplier_name)
		if len(name) > 140:
			name = name[:137] + "..."
		original = name
		counter = 1
		while frappe.db.exists("Bank Integration Supplier", name):
			suffix = f"-{counter}"
			name = original[: 140 - len(suffix)] + suffix
			counter += 1
		self.name = name
