from odoo import models, fields, api


class EncoachPassage(models.Model):
    _name = 'encoach.passage'
    _description = 'Reading Passage'

    exam_type = fields.Selection([
        ('academic', 'Academic'),
        ('general_training', 'General Training'),
        ('general_english', 'General English'),
    ], required=True)
    section_num = fields.Integer(required=True)
    topic_category = fields.Char(size=100, required=True)
    body_text = fields.Text(required=True)
    difficulty = fields.Selection([
        ('easy', 'Easy'),
        ('medium', 'Medium'),
        ('hard', 'Hard'),
    ], required=True)
    status = fields.Selection([
        ('draft', 'Draft'),
        ('active', 'Active'),
        ('retired', 'Retired'),
        ('flagged', 'Flagged'),
    ], default='draft', required=True)
    word_count = fields.Integer(compute='_compute_word_count', store=True)
    ai_generated = fields.Boolean(default=False)
    approved = fields.Boolean(default=False)
    ielts_certified = fields.Boolean(default=False)
    generation_brief = fields.Text()
    validation_errors = fields.Text()
    cefr_level = fields.Selection([
        ('pre_a1', 'Pre-A1'),
        ('a1', 'A1'),
        ('a2', 'A2'),
        ('b1', 'B1'),
        ('b2', 'B2'),
        ('c1', 'C1'),
        ('c2', 'C2'),
    ])

    @api.depends('body_text')
    def _compute_word_count(self):
        for record in self:
            record.word_count = len(record.body_text.split()) if record.body_text else 0
