<?php

namespace App\Classes\Modules\KeyValuePairs\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\KeyValuePairs\DataTransferObjects\KeyValuePairObject;
use App\Classes\Modules\KeyValuePairs\DataTransferObjects\UpdateChatGPTPromptDTO;
use App\Classes\Modules\KeyValuePairs\Standards\Rules\CanUpdateChatGPTPrompt;
use App\Classes\Modules\KeyValuePairs\Services\FetchesKeyValuePair;
use App\Classes\Modules\KeyValuePairs\Services\UpdatesKeyValuePair;
use App\Http\Resources\ChatGPTPromptResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


class UpdateChatGPTPromptKVPLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated ChatGPT Prompt',
            'message' => 'You have successfully updated the ChatGPT prompt'
        ];
    }

    /** @var CanUpdateChatGPTPrompt */
    private $canUpdateChatGPTPrompt;

    /** @var UpdatesKeyValuePair */
    private $updatesKeyValuePair;

    /** @var FetchesKeyValuePair */
    private $fetchesKeyValuePair;

    /**
     * UpdateChatGPTPromptKVPLogic constructor.
     * @param CanUpdateChatGPTPrompt $canUpdateChatGPTPrompt
     * @param UpdatesKeyValuePair $updatesKeyValuePair
     * @param FetchesKeyValuePair $fetchesKeyValuePair
     */
    public function __construct(CanUpdateChatGPTPrompt $canUpdateChatGPTPrompt, UpdatesKeyValuePair $updatesKeyValuePair, FetchesKeyValuePair $fetchesKeyValuePair)
    {
        $this->canUpdateChatGPTPrompt = $canUpdateChatGPTPrompt;
        $this->updatesKeyValuePair = $updatesKeyValuePair;
        $this->fetchesKeyValuePair = $fetchesKeyValuePair;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {
        $dto = new UpdateChatGPTPromptDTO($request->all());
        $this->canUpdateChatGPTPrompt->passes($dto);

        $kvp = $this->fetchesKeyValuePair->execute(['id' => $dto->getId()]);
        $keyValuePairObject = new KeyValuePairObject($kvp->key, $dto->getPrompt());
        $this->updatesKeyValuePair->execute($kvp, $keyValuePairObject);

        return $this->resourceResponse(new ChatGPTPromptResource($kvp));
    }
}
