<?php

namespace LaraBB\Group\Actions\Admin;

use LaraBB\Group\Tasks\FindTask as FindGroupTask;
use LaraBB\Group\Tasks\UpdateTask;
use LaraBB\Group\UI\Web\Requests\Update;

/**
 * Class UpdateAction
 * @package LaraBB\Group\Actions\Admin
 */
class UpdateAction
{
    /**
     * UpdateAction constructor.
     * @param FindGroupTask $getGroupTask
     * @param UpdateTask $updateGroupTask
     */
    public function __construct(
        private readonly FindGroupTask $getGroupTask,
        private readonly UpdateTask $updateGroupTask
    ) {

    }

    /**
     * @param Update $request
     * @return bool
     */
    public function run(Update $request): bool
    {
        $group = $this->getGroupTask->byUuid($request->post('uuid'));

        return $this->updateGroupTask->run($group, $this->prepareGroupData($request));
    }

    /**
     * @param Update $request
     * @return array
     */
    private function prepareGroupData(Update $request): array
    {
        return [
            'updated_uuid' => $request->user()->uuid,
            'group'        => $request->post('group'),
            'color'        => $request->post('color'),
            'priority'     => $request->post('priority')
        ];
    }
}
