<?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\UpdateClaudePromptDTO;
use App\Classes\Modules\KeyValuePairs\Standards\Rules\CanUpdateClaudePrompt;
use App\Classes\Modules\KeyValuePairs\Services\FetchesKeyValuePair;
use App\Classes\Modules\KeyValuePairs\Services\UpdatesKeyValuePair;
use App\Http\Resources\ClaudePromptResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


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

    /** @var CanUpdateClaudePrompt */
    private $canUpdateClaudePrompt;

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

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

    /**
     * UpdateClaudePromptKVPLogic constructor.
     * @param CanUpdateClaudePrompt $canUpdateClaudePrompt
     * @param UpdatesKeyValuePair $updatesKeyValuePair
     * @param FetchesKeyValuePair $fetchesKeyValuePair
     */
    public function __construct(CanUpdateClaudePrompt $canUpdateClaudePrompt, UpdatesKeyValuePair $updatesKeyValuePair, FetchesKeyValuePair $fetchesKeyValuePair)
    {
        $this->canUpdateClaudePrompt = $canUpdateClaudePrompt;
        $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 UpdateClaudePromptDTO($request->all());
        $this->canUpdateClaudePrompt->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 ClaudePromptResource($kvp));
    }
}
