# -*- coding: utf-8 -*-

from odoo import models, fields, api
import time
from odoo.exceptions import UserError


class sos_inventory_customers(models.Model):
    _name = 'sos_inventory_customers'
    _description = 'Inventory Customers'
    _rec_name = 'customer_name'
    _order = 'id desc' 

    customer_name = fields.Char(string="Customer Name")
    customer_location = fields.Char(string="Location")
    @api.model
    def create(self, vals):
        new_name = (vals.get('customer_name') or '').lower()

        if new_name and len(new_name) >= 5:
            existing_customers = self.search([])

            for customer in existing_customers:
                existing_name = (customer.customer_name or '').lower()

                # Check all substrings of length 5 or more
                for i in range(len(new_name) - 4):
                    substring = new_name[i:i+5]
                    if substring in existing_name:
                        raise UserError(
                            f"A customer with a similar name already exists: '{customer.customer_name}' "
                            f"(matched substring: '{substring}')"
                        )

        return super(sos_inventory_customers, self).create(vals)