<?php

namespace App\Services\Forms\Feedback;

use App\Events\Feedback\FeedbackFormSubmitted;
use App\Models\Objects\ObjectType;
use App\Services\Forms\FormsService;
use App\Transformers\Objects\ObjectTransformer;
use Illuminate\Http\JsonResponse;

class FeedbackForms extends FormsService {
    public function store(array $data): ?JsonResponse {
        if ($objectType = ObjectType::byUuid($data['type'] ?? null)->first()) {
            $object = $objectType->objects()->create();
            $object->setValues($data);
            if (!empty($data['mailto'])) event(new FeedbackFormSubmitted($object, $data['mailto']));
            return fractal($object, new ObjectTransformer())->respond();
        }
        return null;
    }
}
