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


class ResPartner(models.Model):
    _inherit = 'res.partner'
    external_id=fields.Char('id')
    statut_contact=fields.Selection([('adherent', 'Adhérent'), ('bienfaiteur', 'Bienfaiteur'),('contact', 'Contact'), ('soutien','Soutien') ],'Statut', index=True)
    date_creation_contact=fields.Char(string='Date de création du contact', readonly=True)
    origine=fields.Selection([
       ('boutique','Boutique'),
            ('visiteur','Visiteur'),
            ('vincennes','Vincennes'),
            ('bercy','Bercy'),
            ('salon zen','Salon Zen'),
            ('autre','Autre'),
            ('internet boutique','Internet Boutique'),
            ('conf M ricard','Conf M Ricard'),
            ('visite lzr 09','Visite lzr 09'),
            ('retraite','Retraite'),
            ('amis','Amis'),
            ('centre FPMT','Centre FPMT'),
            ('site réservations','Site réservations'),
            ('réservations','Réservations'),
            ('voyage inde','Voyage inde'),
            ('internet','Internet'),
            ('librairie','Librairie'),
            ('site internet boutique','Site internet boutique'),
            ('portes ouvertes st cosme','Portes ouvertes st cosme'),
            ('t','T'),
            ('maitreya','maitreya'),
            ('zen','Zen'),
            ('Lille','Lille'),
            ('alan wallace','Alan wallace'),
            ('salon Rouen','Salon Rouen'),
            ('stage','Stage'),
            ('b.Actualit','B.Actualit'),
            ('b. Actualit','B. Actualit'),
            ('r','R'),
            ('institut vy','Institut vy'),
            ('initiation kalachakr','Initiation kalachakr'),
            ('yoga','Yoga'),
            ('samsara','Samsara'),
            ('missun@gmail.com','missun@gmail.com'),
            ('reliques','Reliques'),
            ('journal m','Journal m'),
            ('nantes','Nantes'),
            ('retraite vipassana','retraite vipassana'),
            ('presse bouddhiste','Presse bouddhiste'),
            ('réservtions','Réservtions'),
            ('réservation','Réservation'),
            ('site','Site'),
            ('question mail','Question mail'),
            ('interreligieux','interreligieux'),
            ('refuge','Refuge'),
            ('zoom','zoom'),
                    ],'Origine',  index=True)
    
    date_adhesion=fields.Char(string='Date adhesion', readonly=True)
    
    @api.depends('super_member')
    def  _compute_member_status(self):
        for rec in self:
            
            member=rec.env['kalachakra.membership'].search([('partner_id','=',rec.id),('end_date','>=',datetime.now()),('payment_state','=','paid')])
            if member : rec.member_status='member'
            else :rec.member_status='not member'
            if rec.super_member: rec.member_status='super member'

    member_status=fields.Selection(string='member status',selection=[('not member','Not member'),('member','Member'),('super member','Super member')] 
    ,compute="_compute_member_status",store=True)
    
    super_member=fields.Boolean("Super member")

    @api.onchange('super_member')
    def onchange_super_member(self):
        
        self._compute_member_status()
    

    @api.model
    def create(self, values):
        res = super(ResPartner, self).create(values)
        country_france=self.env['res.country'].sudo().search([('name','=','France')])
        if not res.country_id:res.country_id=country_france.id
        return res    