<?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\CreateClaudePromptDTO;
use App\Classes\Modules\KeyValuePairs\Standards\Rules\CanCreateClaudePrompt;
use App\Classes\ValueObjects\Constants\KVPKey;
use App\Http\Resources\ClaudePromptResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

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

    /** @var CanCreateClaudePrompt */
    private $canCreateClaudePrompt;

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

    /**
     * CreateClaudePromptKVPLogic constructor.
     * @param CanCreateClaudePrompt $canCreateClaudePrompt
     * @param CreatesKeyValuePair $createsKeyValuePair
     */
    public function __construct(
        CanCreateClaudePrompt $canCreateClaudePrompt,
        CreatesKeyValuePair $createsKeyValuePair
    ) {
        $this->canCreateClaudePrompt = $canCreateClaudePrompt;
        $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 CreateClaudePromptDTO($request->all());

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

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

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

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