from odoo import models, fields


class EncoachPermission(models.Model):
    _name = "encoach.permission"
    _description = "EnCoach Permission"

    code = fields.Char(required=True, index=True)
    topic = fields.Char()
    legacy_id = fields.Char(index=True)

    _code_unique = models.Constraint(
        "UNIQUE(code)",
        "Permission code must be unique.",
    )

    def to_encoach_dict(self):
        self.ensure_one()
        return {
            "id": self.id,
            "type": self.code,
            "topic": self.topic or "",
            "users": [],
        }
