from odoo import models, fields, api
from odoo.exceptions import UserError, ValidationError,Warning
from psycopg2 import sql, DatabaseError

from werkzeug import utils


class EventType(models.Model):
    _inherit = "event.type"
    _description = 'Event Template'
   
    booking_event = fields.Boolean(string="Booking event", tracking=True)
    booking_option_ids=fields.One2many('booking.option','event_type_id','booking options')
    booking_questionnaire_id=fields.Many2one(
        'booking.questionnaire',
		String='Questionnaire',
        index=True,
        track_visibility='onchange',
        ondelete='cascade'
    )
class EventBookingOptions(models.Model):
    _name='booking.option'
    _description='booking options for events'
    booking_option_id=fields.Many2one('product.product',string='booking options',domain="['|',('booking_option_product','=','True'),('booking_option_product_backoffice','=','True')]")
    name=fields.Char(related='booking_option_id.name')
    
    booking_option_price=fields.Monetary('Price',currency_field='currency_id')
    booking_option_member_price=fields.Monetary('Member price',currency_field='currency_id')
    booking_option_super_member_price=fields.Monetary('Super member price',currency_field='currency_id')
    
    event_type_id = fields.Many2one(
        'event.type',
		String='Event type',
        index=True,
        readonly=True,
        track_visibility='onchange',
        ondelete='cascade'
    )
    event_id = fields.Many2one(
        'event.event',
		String='Event',
        index=True,
        readonly=True,
        track_visibility='onchange',
        ondelete='cascade'
    )


    @api.model
    def _default_currency(self):
        company = self.env['res.company']._company_default_get(
            'event.event')
        return company.currency_id
   
    currency_id = fields.Many2one(
        'res.currency',
        string='Currency',
        required=True,
        track_visibility='onchange',
        ondelete='restrict',
        default=_default_currency
    )
    @api.onchange('booking_option_id')
    def onchange_booking_option(self):
    
        #for option in self:
        if self.booking_option_id:
            bo= self.env['product.product'].search([('id',"=",int(self.booking_option_id))])
            
            self.booking_option_price= bo.lst_price
    
    


    

class BookingEvent(models.Model):
    _inherit = "event.event"
    _description = 'Event'
    questionnaire_id=fields.Many2one('booking.questionnaire','questionnaire')
    question_ids=fields.One2many(
        'event.question',
        'event_id',
        string='Questions'
        
    )
    
    @api.onchange('questionnaire_id')
    def onchange_questionnaire(self):

        #on supprime les question existantes:
        self.question_ids=False
        questions=self.env['booking.question'].search([('questionnaire_id','=',int(self.questionnaire_id))])
        
        #on rappatrie les questions
        for question in questions:
            
            vals={}
            vals['event_id']=self.id
            vals['sequence']=question.sequence
            vals['question']=question.question
            self.question_ids = [(0, 0, vals)]

        


    # booking_option_ids=fields.One2many('booking.option','event_id','booking options')
    
    # @api.model
    # def _default_currency(self):
    #     company = self.env['res.company']._company_default_get(
    #         'event.event')
    #     return company.currency_id

    # currency_id = fields.Many2one(
    #     'res.currency',
    #     string='Currency',
    #     required=True,
    #     states={'done': [('readonly', True)]},
    #     track_visibility='onchange',
    #     ondelete='restrict',
    #     default=_default_currency
    #)
    # booking_price=fields.Monetary('Price',currency_field='currency_id')
    # booking_member_price=fields.Monetary('Member price',currency_field='currency_id')
    # booking_super_member_price=fields.Monetary('Super member price',currency_field='currency_id')
    # booking_down_payment=fields.Monetary('Down payment',currency_field='currency_id')
    # booking_product_id=fields.Many2one('product.product',string='booking product',domain="[('booking_product','=','True')]")
    

    # @api.depends('event_type_id')
    # def _compute_booking_event(self):
    #     self.booking_event=self.event_type_id.booking_event
        
    # @api.onchange('event_type_id')
    # def onchange_state(self):
    #     self.booking_event=self.event_type_id.booking_event
        
    #     booking_option=self.env['booking.option'].search([('event_type_id','=',int(self.event_type_id))])
    #     if booking_option:
    #         on supprime les options existantes:
    #         self.booking_option_ids=False

    #         on rappatrie les options du modèle
    #         for option in booking_option:
    #             vals={}
    #             vals['event_id']=self.id
    #             vals['booking_option_id']=option.booking_option_id
    #             vals['booking_option_price']=option.booking_option_price
    #             vals['currency_id']=option.currency_id
    #             self.booking_option_ids = [(0, 0, vals)]
        
        
    #     if self.event_type_id.booking_questionnaire_id:
            
    #         on supprime les question existantes:
    #         self.question_ids=False
    #         questions=self.env['booking.question'].search([('questionnaire_id','=',int(self.event_type_id.booking_questionnaire_id))])
    #         on rappatrie les question
    #         for question in questions:
    #             vals={}
    #             vals['event_id']=self.id
    #             vals['sequence']=question.sequence
    #             vals['question']=question.question
                
    #             self.question_ids = [(0, 0, vals)]

        #super(event, self).write(vals)
        #self.booking_option_ids=self.event_type_id.booking_option_ids
class event_question(models.Model):
    _name = 'event.question'
    _description = 'event question'
    
    question=fields.Text(string='question')
    sequence = fields.Integer(string="sequence", default=10)
    event_id = fields.Many2one(
        'event.event',
		String='Questionnaire',
        index=True,
        readonly=True,
        track_visibility='onchange',
        ondelete='cascade'
    )

class event_partner_category(models.Model):
    _name = 'event.partner_category'
    _description = 'map partner categories vs member or super-member for pricing'
    
    partner_category_id=fields.Many2one(
        'res.partner.category',
        String='Category',
        index=True,
        track_visibility='onchange',
        ondelete='cascade'
    )
    
    status=fields.Selection(selection=[('member','Member'),('supermember','Super Member')])

class event_membership_product(models.Model):
    _name = 'event.membership_product'
    _description = 'membership product'

    membership_product_id=fields.Many2one('product.product',string='Membership product',domain="[('booking_option_product','=','True')]")

