from odoo import models, fields


class EncoachDiscussionBoard(models.Model):
    _name = 'encoach.discussion.board'
    _description = 'Discussion Board'

    name = fields.Char(required=True)
    course_id = fields.Many2one('op.course', ondelete='cascade')
    batch_id = fields.Many2one('op.batch', ondelete='set null')
    chapter_id = fields.Many2one('encoach.course.chapter', ondelete='set null')
    is_enabled = fields.Boolean(default=True)
    allow_student_posts = fields.Boolean(default=True)
    post_ids = fields.One2many('encoach.discussion.post', 'board_id')
    post_count = fields.Integer(compute='_compute_post_count', store=True)

    def _compute_post_count(self):
        for rec in self:
            rec.post_count = len(rec.post_ids)


class EncoachDiscussionPost(models.Model):
    _name = 'encoach.discussion.post'
    _description = 'Discussion Post'
    _order = 'create_date desc'

    board_id = fields.Many2one('encoach.discussion.board', required=True, ondelete='cascade')
    parent_id = fields.Many2one('encoach.discussion.post', string='Parent', ondelete='cascade')
    child_ids = fields.One2many('encoach.discussion.post', 'parent_id', string='Replies')
    author_id = fields.Many2one('res.users', required=True, ondelete='cascade')
    title = fields.Char()
    content = fields.Text(required=True)
    is_pinned = fields.Boolean(default=False)
    is_resolved = fields.Boolean(default=False)


class EncoachAnnouncement(models.Model):
    _name = 'encoach.announcement'
    _description = 'Announcement'
    _order = 'create_date desc'

    title = fields.Char(required=True)
    content = fields.Text(required=True)
    author_id = fields.Many2one('res.users', required=True, ondelete='cascade')
    course_id = fields.Many2one('op.course', ondelete='set null')
    batch_id = fields.Many2one('op.batch', ondelete='set null')
    priority = fields.Selection([
        ('normal', 'Normal'),
        ('important', 'Important'),
        ('urgent', 'Urgent'),
    ], default='normal')
    is_published = fields.Boolean(default=False)
    published_at = fields.Datetime()
    expires_at = fields.Datetime()
    send_email = fields.Boolean(default=False)


class EncoachMessage(models.Model):
    _name = 'encoach.message'
    _description = 'Direct Message'
    _order = 'create_date desc'

    sender_id = fields.Many2one('res.users', required=True, ondelete='cascade')
    recipient_id = fields.Many2one('res.users', required=True, ondelete='cascade')
    subject = fields.Char(required=True)
    content = fields.Text(required=True)
    is_read = fields.Boolean(default=False)
    read_at = fields.Datetime()
    send_email_copy = fields.Boolean(default=False)
