from odoo import models, fields


class EncoachResourceTag(models.Model):
    _name = 'encoach.resource.tag'
    _description = 'Resource Tag'
    _order = 'name'

    name = fields.Char(required=True, index=True)
    color = fields.Char(size=20, help='Hex color code for UI badge, e.g. #3b82f6')
    description = fields.Text()
    active = fields.Boolean(default=True)
    resource_ids = fields.Many2many(
        'encoach.resource', 'encoach_resource_tag_rel',
        'tag_id', 'resource_id', string='Resources',
    )

    _sql_constraints = [
        ('name_uniq', 'unique(name)', 'Tag name must be unique.'),
    ]

    def to_api_dict(self):
        self.ensure_one()
        return {
            'id': self.id,
            'name': self.name,
            'color': self.color or '#6b7280',
            'description': self.description or '',
            'resource_count': len(self.resource_ids),
        }
