from odoo import fields, models


class XenoLeaveValidationRule(models.Model):
    """A single, admin-manageable registry of every validation that applies
    to a leave request in this system, gathered in one place (the "Leave
    Validations" page). Three kinds:

      - 'toggle'  : a genuine policy knob this system owns -- the enforcement
                    code in hr.leave checks `enabled` before raising, so an
                    administrator can switch it off/on here with no redeploy
                    (see hr_leave._xeno_validation_enabled).
      - 'per_type': enforced, but its parameters live on each Leave Type
                    (e.g. advance-notice days, approval routing) -- managed
                    there, listed here for visibility.
      - 'core'    : always enforced by Odoo core / an integrity guarantee of
                    the approval chain; shown for completeness, not switchable.

    Rows are seeded from data/leave_validation_rules.xml. That file
    deliberately does NOT set `enabled`, so re-running the module upgrade
    refreshes the descriptions/labels while leaving each rule's on/off state
    exactly as the administrator last set it.
    """

    _name = "xeno.leave.validation.rule"
    _description = "Leave Validation Rule"
    _order = "sequence, id"

    code = fields.Char(
        required=True,
        help="Stable key the enforcement code looks up (do not change).")
    name = fields.Char(required=True, translate=True)
    category = fields.Selection(
        [
            ("submission", "On Submission"),
            ("editing", "On Editing"),
            ("approval", "On Approval"),
            ("balance", "Balance / Allocation"),
        ],
        required=True, default="submission",
        help="When during a leave request's life this validation applies.")
    kind = fields.Selection(
        [
            ("toggle", "Toggleable here"),
            ("per_type", "Configured per Leave Type"),
            ("core", "Always enforced (Odoo core / integrity)"),
        ],
        required=True, default="toggle",
        help="Whether this rule can be switched off here, is parameterised "
             "on each Leave Type, or is always enforced.")
    enabled = fields.Boolean(
        default=True,
        help="Only has effect for 'Toggleable here' rules -- switch a policy "
             "off to stop the system enforcing it, no redeploy needed.")
    description = fields.Text(translate=True)
    manage_hint = fields.Char(
        string="Where to Manage", translate=True,
        help="For rules configured elsewhere, where an administrator sets "
             "their parameters.")
    sequence = fields.Integer(default=10)

    _sql_constraints = [
        ("code_uniq", "unique(code)", "Each validation rule code must be unique."),
    ]
