# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2016 Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################

from odoo import _, api, fields, models
from odoo.exceptions import AccessError, UserError, ValidationError
import datetime as dt

class EventRegistration(models.Model):
    _inherit = "event.registration"

    @api.multi
    def _check_auto_confirmation(self):
        """Forbid auto-confirmation of new Attendees.
        """
        return False

    @api.one
    def button_reg_close(self):
        """ Close Registration 
        Extension : Allow to mark Attendee as "Attended" before 30 minutes of event starts
        """
        today_date = dt.datetime.strptime(fields.Datetime.now(), '%Y-%m-%d %H:%M:%S')
        event_30min_before = dt.datetime.strptime(self.event_id.date_begin, '%Y-%m-%d %H:%M:%S') - dt.timedelta(minutes=30)
        if today_date >= event_30min_before:
            self.write({'state': 'done', 'date_closed': fields.Datetime.now()})
        else:
            raise UserError(_("You must wait for the starting day of the event OR 30 minutes before event starts to do this action."))
			
    employee_id = fields.Many2one('hr.employee', 'Manager', track_visibility='onchange')
    state = fields.Selection([
        ('draft', 'Unconfirmed'), ('cancel', 'Cancelled'), ('invited', 'Invited'),
        ('open', 'Confirmed'), ('done', 'Attended')],
        string='Status', default='draft', readonly=True, copy=False, track_visibility='onchange')

    @api.one
    def action_set_invited(self):
        return self.write({'state': 'invited'})