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

$GLOBALS["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"] = (@$GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"] ?: array()) + array("_fab5a218" . "8c706d282" . "c3aea92f0" . "ccbd01" => base64_decode("bWVzc2FnZX" . "M="), "_8285a80c1dc92410906d10e04" . "8fe395f" => base64_decode("cGFy" . "dF90" . "eXBl"), "v" . "a" . "l" . "u" . "e" => base64_decode("dmFsd" . "WU="), "_4d707fab93beb" . "b00bbe69e81596" . "97c90" => base64_decode(""), "_d1071a032215" . "f0eb5c4a8ef17" . "5f7049d" => base64_decode("cm9s" . "ZQ=="), "_d10ce5ca19ad2ee8e4b9" . "8309ad43541b" => base64_decode("Y3JlYX" . "RlZF9h" . "dA=="), "titl" . "e" => base64_decode("dGl" . "0bG" . "U="), "_3877cdf57a61e" . "49b56aeab7af93" . "0ee82" => base64_decode("dXNlcl9pZA" . "=="), "_948976cc86bfda1b" . "4ba9629a8e31d55f" => base64_decode("IT0" . "="), "i" . "d" => base64_decode("a" . "W" . "Q" . "="), "_25867e3e3db28969253" . "f3a370c3166ba" => base64_decode("ZG" . "Vz" . "Yw" . "=="), "_fe2f527ebd1bc77797bcfc85685d" . "3837" => base64_decode("Y2hhdF9p" . "ZA=="), "_a2bf5e9dcb99c4" . "c9637ad544d228d" . "cae" => base64_decode("dXBkY" . "XRlZF" . "9hdA=" . "="));
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["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_3877cdf57a61e49b56a" . "eab7af930ee82"], $context->GetUserId())->where($GLOBALS["__65da1b1ec2d6653e8a0ab78" . "48e536215"]["i" . "d"], $id)->with($GLOBALS["__65da1b1ec2d6653" . "e8a0ab7848e536215"]["_fab5a2188c706d28" . "2c3aea92f0ccbd01"])->first();
    }
    
    
    
    
    
    
    
    
    public function getChat(SecurityContext $context, string $id) : ?AiChat
    {
        return $this->mapper->where($GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_3877cdf57a61e49b56a" . "eab7af930ee82"], $context->GetUserId())->where($GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["i" . "d"], $id)->first();
    }
    
    
    
    
    
    
    
    public function getChats(SecurityContext $context)
    {
        return $this->mapper->where($GLOBALS["__65da1b1ec2d6653e8a0ab7" . "848e536215"]["_3877cdf57a61e49b5" . "6aeab7af930ee82"], $context->GetUserId())->whereNotNull($GLOBALS["__65da1b1ec2d6653e8a0ab" . "7848e536215"]["ti" . "tl" . "e"])->where($GLOBALS["__65da1b1ec2d665" . "3e8a0ab7848e5362" . "15"]["titl" . "e"], $GLOBALS["__65da1b1ec2d6653e8a" . "0ab7848e536215"]["_948976cc86bfda1b4" . "ba9629a8e31d55f"], $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5362" . "15"]["_4d707fab93beb" . "b00bbe69e81596" . "97c90"])->orderBy($GLOBALS["__65da1b1ec2d6653e8a0ab7848" . "e536215"]["_d10ce5ca1" . "9ad2ee8e4b" . "98309ad435" . "41b"], $GLOBALS["__65da1b1ec2d6653e8a0" . "ab7848e536215"]["_25867e3e3db2896" . "9253f3a370c3166b" . "a"])->get()->all();
    }
    
    
    
    
    
    
    
    public function newChat(SecurityContext $context) : AiChat
    {
        
        
        
        
        $_dace5e187fa775d4aabf1747566801d7 = $this->newInstance();
        $_dace5e187fa775d4aabf1747566801d7->user_id = $context->GetUserId();
        $_dace5e187fa775d4aabf1747566801d7->title = $GLOBALS["__65da1b1ec2d6653e" . "8a0ab7848e536215"]["_4d707fab93bebb00bbe69e8159697" . "c90"];
        
        
        $_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["__65da1b1ec2d" . "6653e8a0ab784" . "8e536215"]["_fe2f527ebd1bc77797bc" . "fc85685d3837"] => $_dace5e187fa775d4aabf1747566801d7->id, $GLOBALS["__65da1b1ec2d6653e8a0ab784" . "8e536215"]["_d1071a032215f0eb5c4a8" . "ef175f7049d"] => $_d962cfd1735adb2dfba46f69c7f15cb8->getRole(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e5" . "36215"]["_8285a80c1dc92410906d10e048fe39" . "5f"] => $_d962cfd1735adb2dfba46f69c7f15cb8->getName(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["v" . "a" . "l" . "u" . "e"] => $_d962cfd1735adb2dfba46f69c7f15cb8->getValue(), $GLOBALS["__65da1b1ec2d6653e8a0a" . "b7848e536215"]["_d10ce5ca19ad2ee8e4b98309ad43541" . "b"] => new Date(), $GLOBALS["__65da1b1ec2d6653e8a0ab7848e53621" . "5"]["_a2bf5e9dcb99" . "c4c9637ad544d" . "228dcae"] => 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);
    }
}