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

class ReservationHoliday(models.Model):
    _name = 'reservation.holiday'
    _description = 'Reservation Holiday/Override'
    _order = 'date desc'

    name = fields.Char(string='Description', required=True, placeholder="e.g. Christmas Day")
    date = fields.Date(string='Date', required=True)
    
    is_closed = fields.Boolean(string='Closed for Reservations', default=True)
    
    opening_time = fields.Float(string='Override Opening Time', help="Leave empty to use regular schedule if not closed")
    closing_time = fields.Float(string='Override Closing Time')

    @api.constrains('opening_time', 'closing_time', 'is_closed')
    def _check_times(self):
        for rec in self:
            if not rec.is_closed and (rec.opening_time or rec.closing_time):
                if rec.opening_time >= rec.closing_time:
                    raise ValidationError(_("Override Opening time must be before closing time."))

    _sql_constraints = [
        ('date_unique', 'unique(date)', 'An override for this date already exists!')
    ]
