from datetime import datetime as dt, timezone as tz

from common.models.interaction import EveAIDataCapsule
from common.extensions import db
from common.utils.model_logging_utils import set_logging_information, update_logging_information


class CapsuleServices:
    @staticmethod
    def push_capsule_data(chat_session_id: str, type: str, type_version: str, configuration: dict, data: dict):
        capsule = EveAIDataCapsule.query.filter_by(chat_session_id=chat_session_id, type=type, type_version=type_version).first()
        if capsule:
            # Update bestaande capsule als deze al bestaat
            capsule.configuration = configuration
            capsule.data = data
            update_logging_information(capsule, dt.now(tz.utc))
        else:
            # Maak nieuwe capsule aan als deze nog niet bestaat
            capsule = EveAIDataCapsule(chat_session_id=chat_session_id, type=type, type_version=type_version,
                                       configuration=configuration, data=data)
            set_logging_information(capsule, dt.now(tz.utc))
            db.session.add(capsule)

        db.session.commit()
        return capsule