<?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\Services\CreatesKeyValuePair;
use App\Classes\Modules\KeyValuePairs\DataTransferObjects\CreateChatGPTPromptDTO;
use App\Classes\Modules\KeyValuePairs\Standards\Rules\CanCreateChatGPTPrompt;
use App\Classes\ValueObjects\Constants\KVPKey;
use App\Http\Resources\ChatGPTPromptResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateChatGPTPromptKVPLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Created ChatGPT Prompt',
            'message' => 'You have successfully created a new ChatGPT Prompt'
        ];
    }

    /** @var CanCreateChatGPTPrompt */
    private $canCreateChatGPTPrompt;

    /** @var CreatesKeyValuePair */
    private $createsKeyValuePair;

    /**
     * CreateChatGPTPromptKVPLogic constructor.
     * @param CanCreateChatGPTPrompt $canCreateChatGPTPrompt
     * @param CreatesKeyValuePair $createsKeyValuePair
     */
    public function __construct(
        CanCreateChatGPTPrompt $canCreateChatGPTPrompt,
        CreatesKeyValuePair $createsKeyValuePair
    ) {
        $this->canCreateChatGPTPrompt = $canCreateChatGPTPrompt;
        $this->createsKeyValuePair = $createsKeyValuePair;
    }

    /**
     * @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 CreateChatGPTPromptDTO($request->all());

        $this->canCreateChatGPTPrompt->passes($dto);

        $key = KVPKey::CHATGPT_PROMPT_PREFIX . strtoupper(str_replace(' ', '_', $dto->getTitle()));
        $keyValuePairObject = new KeyValuePairObject($key, $dto->getPrompt());

        $kvp = $this->createsKeyValuePair->execute(null, $keyValuePairObject);

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