from odoo import fields, models


class TierChannelMixin(models.AbstractModel):
    _name = 'tier.channel.mixin'
    _description = 'Tier Channel Mixin'
    _abstract = True

    credential_scope = fields.Selection(
        selection=[('company', 'Company'), ('user', 'User')],
        string='Credential Scope',
        default='company',
        required=True,
    )

    def channel_authenticate(self):
        """Authenticate with the channel. Returns True by default (e.g. Email needs no auth)."""
        return True

    def channel_send_message(self, message):
        """Send a message through this channel. Must be overridden by subclasses."""
        raise NotImplementedError(
            f"channel_send_message() is not implemented for {self._name}"
        )

    def channel_receive_message(self, payload):
        """Receive a message from this channel. Must be overridden by subclasses."""
        raise NotImplementedError(
            f"channel_receive_message() is not implemented for {self._name}"
        )

    def _get_credentials(self):
        """Return credentials based on credential_scope."""
        if self.credential_scope == 'user':
            return self._get_user_credentials(self.env.user)
        return self._get_company_credentials(self.env.company)

    def _get_user_credentials(self, user):
        """Return user-level credentials. Override in subclasses."""
        return {}

    def _get_company_credentials(self, company):
        """Return company-level credentials. Override in subclasses."""
        return {}
