from odoo import models, fields


class EncoachRole(models.Model):
    _name = "encoach.role"
    _description = "EnCoach Role"

    name = fields.Char(required=True)
    entity_id = fields.Many2one("encoach.entity", required=True, ondelete="cascade")
    permission_ids = fields.Many2many("encoach.permission", string="Permissions")
    is_default = fields.Boolean(default=False)
    legacy_id = fields.Char(index=True)

    def to_encoach_dict(self):
        self.ensure_one()
        return {
            "id": self.id,
            "label": self.name,
            "entityID": self.entity_id.id,
            "permissions": [p.code for p in self.permission_ids if p.code],
            "isDefault": self.is_default,
        }
