<?php

namespace App\Actions\Category;

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

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

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

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

        return $result;
    }
}
