from odoo import _, api, fields, models
from odoo.exceptions import UserError

HR_OFFICER_GROUP = "hr_holidays.group_hr_holidays_user"


class XenoLeaveHrCreate(models.TransientModel):
    _name = "xeno.leave.hr.create"
    _description = (
        "HR: create a leave request directly on behalf of one or more "
        "employees -- no approval chain, immediately approved, past dates "
        "allowed. Mirrors XENHR's storeForEmployee (HR/admin creating on "
        "someone else's behalf skips both the approval workflow and the "
        "advance-notice/backdate timing limits)."
    )

    employee_ids = fields.Many2many("hr.employee", string="Employee", required=True)
    holiday_status_id = fields.Many2one("hr.leave.type", string="Leave Type", required=True)
    xeno_flexible_duration = fields.Boolean(
        related="holiday_status_id.xeno_flexible_duration", readonly=True)
    xeno_request_unit = fields.Selection(
        [
            ("full_day", "Full-day Leave"),
            ("part_time", "Part-time Leave"),
            ("first_half", "First-half Day Leave"),
            ("second_half", "Second-half Day Leave"),
        ],
        string="Leave Type Basis", default="full_day", required=True,
    )
    request_hour_from = fields.Float(string="Start Time")
    request_hour_to = fields.Float(string="End Time")
    request_date_from = fields.Date(string="Start Date", required=True)
    request_date_to = fields.Date(string="End Date", required=True)
    name = fields.Text(string="Reason")

    @api.onchange("holiday_status_id")
    def _onchange_holiday_status_id(self):
        if not self.xeno_flexible_duration:
            self.xeno_request_unit = "full_day"

    def action_apply(self):
        self.ensure_one()
        if not self.env.user.has_group(HR_OFFICER_GROUP):
            raise UserError(_(
                "Only Time Off Officers can create a leave on behalf of "
                "an employee."))
        if self.request_date_to < self.request_date_from:
            raise UserError(_("End date must be on or after start date."))
        if self.xeno_request_unit == "part_time" and self.request_hour_to <= self.request_hour_from:
            raise UserError(_("End time must be after start time."))

        Leave = self.env["hr.leave"]
        created = self.env["hr.leave"]
        for employee in self.employee_ids:
            vals = {
                "employee_id": employee.id,
                "holiday_status_id": self.holiday_status_id.id,
                "xeno_request_unit": self.xeno_request_unit,
                "request_date_from": self.request_date_from,
                "request_date_to": self.request_date_to,
                "name": self.name,
            }
            if self.xeno_request_unit == "part_time":
                vals["request_hour_from"] = self.request_hour_from
                vals["request_hour_to"] = self.request_hour_to
            created |= Leave.with_context(xeno_hr_direct_create=True).create(vals)
        created._xeno_hr_created_approve()
        return {"type": "ir.actions.act_window_close"}
