from odoo import fields, models


class HrEmployee(models.Model):
    _inherit = "hr.employee"

    xeno_resignation_ids = fields.One2many(
        "xeno.resignation", "employee_id", string="Resignation Records"
    )
    xeno_resignation_count = fields.Integer(compute="_compute_xeno_resignation_count")

    def _compute_xeno_resignation_count(self):
        counts = self.env["xeno.resignation"]._read_group(
            [("employee_id", "in", self.ids)], ["employee_id"], ["__count"]
        )
        count_by_employee = {employee.id: count for employee, count in counts}
        for employee in self:
            employee.xeno_resignation_count = count_by_employee.get(employee.id, 0)

    def action_view_xeno_resignations(self):
        self.ensure_one()
        return {
            "type": "ir.actions.act_window",
            "name": "Resignations",
            "res_model": "xeno.resignation",
            "view_mode": "list,form",
            "domain": [("employee_id", "=", self.id)],
            "context": {"default_employee_id": self.id},
        }
