# This Python file uses the following encoding: utf-8
from ..PyqcrmFlags import PyqcrmFlags
import mariadb
from PySide6.QtCore import QObject, Signal
from lib.domain.BaseModel import database


class UserDAO(QObject):
    noDbConnection = Signal(str)
    __cursor = None

    def __init__(self):
        super().__init__()
        self.__con = database.connection()
        if self.__con:
            self.__cur = self.__con.cursor()

    def createUser(self, username, password, info, role=PyqcrmFlags.USER):
        user_created = False
        try:
            if self.__cur:
                self.__cur.callproc("createUser", (username, password, info, role))
                self.__con.commit()
                user_created = True
        except mariadb.Error as e:
            print(f"Error: {e}")
            print(e.errno)
            user_created = False
            self.noDbConnection.emit(str(e))
        finally:
            return user_created

    def getUser(self, username):
        try:
            if self.__cur:
                self.__cur.callproc("getUser", (username,))
                return self.__cur.fetchone()
            else:
                return None
        except mariadb.Error as e:
            print(str(e))
            self.noDbConnection.emit(str(e))
