<?php
namespace Claromentis\Core\Ai\Repository;

$GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"] = (@$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"] ?: array()) + array("_25867e3e3db28969253f3a37" . "0c3166ba" => base64_decode("ZG" . "Vz" . "Yw" . "=="), "_948976cc86bfda1b4ba9629a8e31d" . "55f" => base64_decode("I" . "T" . "0" . "="), "_fe2f527" . "ebd1bc77" . "797bcfc8" . "5685d383" . "7" => base64_decode("Y2hhdF" . "9pZA=="), "_3877cdf57a61e4" . "9b56aeab7af930e" . "e82" => base64_decode("dXNlcl" . "9pZA=="), "_fab5a2188c706" . "d282c3aea92f0c" . "cbd01" => base64_decode("bWVzc2FnZ" . "XM="), "ti" . "tl" . "e" => base64_decode("dGl0b" . "GU="), "i" . "d" => base64_decode("a" . "W" . "Q" . "="), "val" . "ue" => base64_decode("dmF" . "sdW" . "U="), "_a2bf5e9dc" . "b99c4c9637" . "ad544d228d" . "cae" => base64_decode("dXBkYXR" . "lZF9hdA" . "=="), "_d1071a032215f0eb5c4a8ef175f7049" . "d" => base64_decode("cm9" . "sZQ" . "=="), "_8285a80c1dc92410" . "906d10e048fe395f" => base64_decode("cGFydF90eXB" . "l"), "_d10ce5ca19ad2ee8e4b98309ad4" . "3541b" => base64_decode("Y3Jl" . "YXRl" . "ZF9h" . "dA=="), "_4d707fab93bebb00bbe" . "69e8159697c90" => base64_decode(""));
use Analogue\ORM\System\Mapper;
use Claromentis\Core\Ai\Entity\AiChat;
use Claromentis\Core\Ai\Entity\AiChatHistory;
use Claromentis\Core\Ai\Events\AiChatHistoryBeforeAddEvent;
use Claromentis\Core\Ai\Events\AiEventType;
use Claromentis\Core\Ai\Parts\PartInterface;
use Claromentis\Core\Event\EventDispatcherAwareTrait;
use Claromentis\Core\Event\NullEventDispatcher;
use Claromentis\Core\ORM\Repository;
use Claromentis\Core\Security\SecurityContext;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Date;
class AiChatRepository extends Repository
{
    use EventDispatcherAwareTrait;
    
    
    
    
    
    
    
    public function __construct(Mapper $_dabd1eb8030f2c29327a192cddfd85a9, ?EventDispatcherInterface $_b497ce6f2af10f67516aed0ecf34d8cf = null)
    {
        parent::__construct($_dabd1eb8030f2c29327a192cddfd85a9);
        $this->setEventDispatcher($_b497ce6f2af10f67516aed0ecf34d8cf ?? new NullEventDispatcher());
    }
    
    
    
    
    
    
    
    
    public function getChatWithHistory(SecurityContext $context, string $id) : ?AiChat
    {
        return $this->mapper->where($GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_3877cdf57a61" . "e49b56aeab7af" . "930ee82"], $context->GetUserId())->where($GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["i" . "d"], $id)->with($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_fab5a2188c706d282c3ae" . "a92f0ccbd01"])->first();
    }
    
    
    
    
    
    
    
    
    public function getChat(SecurityContext $context, string $id) : ?AiChat
    {
        return $this->mapper->where($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_3877cdf57a61e49b56aeab7af930" . "ee82"], $context->GetUserId())->where($GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["i" . "d"], $id)->first();
    }
    
    
    
    
    
    
    
    public function getChats(SecurityContext $context)
    {
        return $this->mapper->where($GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_3877cdf57a61e49b56aeab" . "7af930ee82"], $context->GetUserId())->whereNotNull($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["titl" . "e"])->where($GLOBALS["__65da1b1ec2" . "d6653e8a0ab7" . "848e536215"]["titl" . "e"], $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_948976cc8" . "6bfda1b4ba" . "9629a8e31d" . "55f"], $GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_4d707fab93bebb00bbe69e8159" . "697c90"])->orderBy($GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_d10ce5ca19ad" . "2ee8e4b98309a" . "d43541b"], $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53" . "6215"]["_25867e3e3db2896925" . "3f3a370c3166ba"])->get()->all();
    }
    
    
    
    
    
    
    
    public function newChat(SecurityContext $context) : AiChat
    {
        
        
        
        
        $_dace5e187fa775d4aabf1747566801d7 = $this->newInstance();
        $_dace5e187fa775d4aabf1747566801d7->user_id = $context->GetUserId();
        $_dace5e187fa775d4aabf1747566801d7->title = $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_4d707fab93bebb00bbe69e8" . "159697c90"];
        
        
        $_dace5e187fa775d4aabf1747566801d7->created_at = new Date();
        $_dace5e187fa775d4aabf1747566801d7->updated_at = new Date();
        return $this->store($_dace5e187fa775d4aabf1747566801d7);
    }
    
    
    
    
    
    
    
    
    
    public function addHistory(SecurityContext $context, AiChat $_dace5e187fa775d4aabf1747566801d7, PartInterface $_d962cfd1735adb2dfba46f69c7f15cb8) : AiChat
    {
        $_a82e7c9cba13670954fbe01d604c242a = new AiChatHistory();
        $_a82e7c9cba13670954fbe01d604c242a->setEntityAttributes([$GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_fe2f527ebd1bc7" . "7797bcfc85685d3" . "837"] => $_dace5e187fa775d4aabf1747566801d7->id, $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_d1071a032215f0eb5c4a8" . "ef175f7049d"] => $_d962cfd1735adb2dfba46f69c7f15cb8->getRole(), $GLOBALS["__65da1b1ec2d6" . "653e8a0ab7848e" . "536215"]["_8285a80c1dc92410906d10e048fe395" . "f"] => $_d962cfd1735adb2dfba46f69c7f15cb8->getName(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["valu" . "e"] => $_d962cfd1735adb2dfba46f69c7f15cb8->getValue(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_d10ce5ca19ad2ee8e4b98309a" . "d43541b"] => new Date(), $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_a2bf5e9dcb99c4c9637ad5" . "44d228dcae"] => new Date()]);
        $event = new AiChatHistoryBeforeAddEvent($_a82e7c9cba13670954fbe01d604c242a, $context);
        $this->dispatcher->dispatch($event, AiEventType::AI_CHAT_HISTORY_BEFORE_CREATE);
        $_dace5e187fa775d4aabf1747566801d7->history->add($event->getSubject());
        return $this->mapper->store($_dace5e187fa775d4aabf1747566801d7);
    }
    
    
    
    
    
    
    
    
    public function updateTitle(AiChat $_dace5e187fa775d4aabf1747566801d7, string $title) : AiChat
    {
        $_dace5e187fa775d4aabf1747566801d7->title = $title;
        return $this->mapper->store($_dace5e187fa775d4aabf1747566801d7);
    }
}