# -*- coding: utf-8 -*-
import logging
from odoo import models, fields, _
from odoo.exceptions import UserError
from odoo.osv import expression

logger = logging.getLogger(__name__)


class AccountAvataxUniqueCode(models.AbstractModel):

    _name = 'account.avatax.unique.code'
    _description = 'Mixin to generate unique ids for Avatax'

    avatax_unique_code = fields.Char(
        "Avalara Code",
        compute="_compute_avatax_unique_code",
        search="_search_avatax_unique_code",
        store=False,
        help="Use this code to cross-reference in the Avalara portal."
    )

    def _get_avatax_description(self):

        raise NotImplementedError()

    def _compute_avatax_unique_code(self):
        for record in self:
            record.avatax_unique_code = '%s %s' % (record._get_avatax_description(), record.id)

    def _search_avatax_unique_code(self, operator, value):
        unsupported_operators = ('in', 'not in', '<', '<=', '>', '>=')
        if operator in unsupported_operators or not isinstance(value, str):
            raise UserError(_("Search operation not supported"))

        value = value.lower()

        prefix = self._get_avatax_description().lower() + " "
        if value.startswith(prefix):
            value = value[len(prefix):]

        if operator in ('=', '!=') and not value.isdigit():
            return expression.FALSE_DOMAIN

        if not value:
            return expression.FALSE_DOMAIN

        return [('id', operator, value)]
