<?php

namespace App\Actions\Category;

use App\DTOs\CategoryData;
use App\Events\ModelChanged;
use App\Models\Category;
use App\Repositories\Contracts\CategoryRepositoryInterface;

class CreateCategoryAction
{
    public function __construct(private CategoryRepositoryInterface $repository) {}

    public function execute(CategoryData $data): Category
    {
        /** @var Category */
        $result = $this->repository->create($data->toArray());

        ModelChanged::dispatch(Category::class, 'created');

        return $result;
    }
}
