import json

from PySide6.QtCore import QObject, Signal

from lib.domain.BaseModel import database


class EmployeeDAO(QObject):
    newEmployeeAdded = Signal(bool)

    def __init__(self):
        super().__init__()
        self._connection = database.connection()

    def getEmployees(self, enc_key, criterion="Alle", processed=False, fired=False, every_state=True):
        cursor = self._connection.cursor()
        try:
            cursor.callproc("getEmployeeTable", (criterion, processed, fired, every_state, enc_key,))
            all_cols = [desc[0] for desc in cursor.description]
            result = cursor.fetchall(), all_cols
            return result
        finally:
            cursor.close()

    def fetchApplicant(self, employee_id, enc_key=None) -> dict:
        cursor = self._connection.cursor(dictionary=True)
        try:
            cursor.callproc("getApplicant", (employee_id, enc_key))
            it = cursor.fetchone()
            return it
        finally:
            cursor.close()

    def addApplicant(self, data, enc_key, applicant=True):
        cursor = self._connection.cursor()
        try:
            cursor.callproc("addApplicant", (json.dumps(data), applicant, enc_key,))
            self._connection.commit()
            self.newEmployeeAdded.emit(True)
        finally:
            cursor.close()
