import odoorpc
from config import ODOO_HOST, ODOO_DB, ODOO_USER, ODOO_PASSWORD

class OdooConnection:
    """Gère la connexion à l'instance Odoo"""
    
    def __init__(self):
        self.odoo = None
        self.connected = False
    
    def connect(self):
        """Établit la connexion à Odoo"""
        try:
            self.odoo = odoorpc.ODOO(ODOO_HOST, port=443, protocol='jsonrpc+ssl')
            print(f"Connexion réussie à {ODOO_HOST}")
            
            self.odoo.login(ODOO_DB, ODOO_USER, ODOO_PASSWORD)
            print(f"Authentifié en tant que {ODOO_USER}")
            
            self.connected = True
            return True
        except odoorpc.error.RPCError as e:
            print(f"Erreur RPC Odoo : {e}")
            return False
        except Exception as e:
            print(f"Erreur inattendue : {e}")
            return False
    
    def get_odoo_instance(self):
        """Retourne l'instance Odoo connectée"""
        if not self.connected:
            self.connect()
        return self.odoo 