from odoo import fields, models


class XenoAttendanceDayoff(models.Model):
    """HR-granted day off for a single employee-day -- e.g. in exchange for
    overtime or extra-day working. On the Monthly Attendance report, that
    cell renders as "Off" (its own colour, like a holiday/weekend) instead
    of blank/Absent, and is excluded from the late-minutes total. No
    check-in/out is expected from the employee that day."""

    _name = "xeno.attendance.dayoff"
    _description = "HR-granted employee day off (e.g. OT/extra-day comp off)"
    _order = "date desc, employee_id"

    employee_id = fields.Many2one("hr.employee", required=True, index=True)
    employee_code = fields.Char(
        related="employee_id.xeno_employee_code", store=True, index=True,
        help="HRSystem employee_code -- the join key the Monthly Attendance "
             "report matches this record against (same as Attendance "
             "Overrides). The chosen employee must already have one set.")
    date = fields.Date(required=True, index=True)
    reason = fields.Char(help="e.g. 'Comp off for OT on 1 Aug 2026' -- shown on the report tooltip.")

    _employee_date_uniq = models.Constraint(
        "unique(employee_id, date)",
        "This employee already has a day off recorded for that date.",
    )

    def name_get(self):
        return [(r.id, "%s @ %s" % (r.employee_id.name, r.date)) for r in self]
