from odoo import models, fields


class EncoachFaqCategory(models.Model):
    _name = 'encoach.faq.category'
    _description = 'FAQ Category'
    _order = 'sequence, id'

    name = fields.Char(required=True)
    sequence = fields.Integer(default=10)
    audience = fields.Selection([
        ('all', 'All'),
        ('student', 'Students'),
        ('teacher', 'Teachers'),
        ('admin', 'Admins'),
    ], default='all')
    is_published = fields.Boolean(default=True)
    item_ids = fields.One2many('encoach.faq.item', 'category_id')
    item_count = fields.Integer(compute='_compute_item_count', store=True)

    def _compute_item_count(self):
        for rec in self:
            rec.item_count = len(rec.item_ids)


class EncoachFaqItem(models.Model):
    _name = 'encoach.faq.item'
    _description = 'FAQ Item'
    _order = 'sequence, id'

    category_id = fields.Many2one('encoach.faq.category', required=True, ondelete='cascade')
    question = fields.Char(required=True)
    answer = fields.Text(required=True)
    sequence = fields.Integer(default=10)
    audience = fields.Selection([
        ('all', 'All'),
        ('student', 'Students'),
        ('teacher', 'Teachers'),
        ('admin', 'Admins'),
    ], default='all')
    is_published = fields.Boolean(default=True)
