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

$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"] = (@$GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"] ?: array()) + array("_a2bf5e9dcb99c4c96" . "37ad544d228dcae" => base64_decode("dXBkY" . "XRlZF" . "9hdA=" . "="), "_fab5a2188c706" . "d282c3aea92f0c" . "cbd01" => base64_decode("bWVzc2Fn" . "ZXM="), "titl" . "e" => base64_decode("dGl0" . "bGU="), "val" . "ue" => base64_decode("dmFs" . "dWU="), "_3877cdf57a61e49b56aeab7af930e" . "e82" => base64_decode("dXNlcl9pZ" . "A=="), "_d1071a032215f0eb5c4a8ef175f" . "7049d" => base64_decode("cm9s" . "ZQ=="), "_8285a80c1dc92410906d10e048f" . "e395f" => base64_decode("cGFydF90" . "eXBl"), "_948976cc86bfda1b4ba96" . "29a8e31d55f" => base64_decode("I" . "T" . "0" . "="), "_d10ce5ca19ad2ee8e4b98309ad4354" . "1b" => base64_decode("Y3JlYX" . "RlZF9h" . "dA=="), "_4d707fab93bebb00bbe69e81596" . "97c90" => base64_decode(""), "_25867e3e3db28969253f3a370" . "c3166ba" => base64_decode("ZGVzY" . "w=="), "_fe2f527e" . "bd1bc7779" . "7bcfc8568" . "5d3837" => base64_decode("Y2h" . "hdF" . "9pZ" . "A=="), "i" . "d" => base64_decode("aWQ" . "="));
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["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_3877cdf57a61e49b5" . "6aeab7af930ee82"], $context->GetUserId())->where($GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["i" . "d"], $id)->with($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_fab5a218" . "8c706d282" . "c3aea92f0" . "ccbd01"])->first();
    }
    
    
    
    
    
    
    
    
    public function getChat(SecurityContext $context, string $id) : ?AiChat
    {
        return $this->mapper->where($GLOBALS["__65da1b1ec2d6653e8a0ab7848e536" . "215"]["_3877cdf57a61e49b56aeab7af930" . "ee82"], $context->GetUserId())->where($GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["i" . "d"], $id)->first();
    }
    
    
    
    
    
    
    
    public function getChats(SecurityContext $context)
    {
        return $this->mapper->where($GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_3877cdf57a61e49b56aeab7" . "af930ee82"], $context->GetUserId())->whereNotNull($GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["t" . "i" . "t" . "l" . "e"])->where($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["t" . "i" . "t" . "l" . "e"], $GLOBALS["__65da1b1ec" . "2d6653e8a0a" . "b7848e53621" . "5"]["_948976cc86bfda" . "1b4ba9629a8e31d" . "55f"], $GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_4d707fab93" . "bebb00bbe69" . "e8159697c90"])->orderBy($GLOBALS["__65da1b1" . "ec2d6653e" . "8a0ab7848" . "e536215"]["_d10ce5ca19ad2ee8e4b9" . "8309ad43541b"], $GLOBALS["__65da1b1ec2d66" . "53e8a0ab7848e53" . "6215"]["_25867e3e3db2896925" . "3f3a370c3166ba"])->get()->all();
    }
    
    
    
    
    
    
    
    public function newChat(SecurityContext $context) : AiChat
    {
        
        
        
        
        $_dace5e187fa775d4aabf1747566801d7 = $this->newInstance();
        $_dace5e187fa775d4aabf1747566801d7->user_id = $context->GetUserId();
        $_dace5e187fa775d4aabf1747566801d7->title = $GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["_4d707fab93bebb00bbe69e8159697c9" . "0"];
        
        
        $_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["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_fe2f527ebd1bc77797bcfc85" . "685d3837"] => $_dace5e187fa775d4aabf1747566801d7->id, $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_d1071a03" . "2215f0eb5" . "c4a8ef175" . "f7049d"] => $_d962cfd1735adb2dfba46f69c7f15cb8->getRole(), $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_8285a80c1dc92410906d10e048fe395" . "f"] => $_d962cfd1735adb2dfba46f69c7f15cb8->getName(), $GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["val" . "ue"] => $_d962cfd1735adb2dfba46f69c7f15cb8->getValue(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_d10ce5ca19ad2ee8e4b98309ad4354" . "1b"] => new Date(), $GLOBALS["__65da1b1e" . "c2d6653e8a" . "0ab7848e53" . "6215"]["_a2bf5e9dcb99c4c9637ad544d2" . "28dcae"] => 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);
    }
}