<?php

namespace LaraBB\Post\Tasks;

use LaraBB\Post\Models\Post;

/**
 * Class UpdatePostTask
 * @package LaraBB\Post\Tasks
 */
class UpdateTask
{
    /**
     * @param Post $post
     * @param $data
     * @return bool
     */
    public function run(Post $post, $data): bool
    {
        $post->updated_uuid = $data['updated_uuid'] ?? $post->updated_uuid;
        $post->thread_uuid  = $data['thread_uuid'] ?? $post->thread_uuid;
        $post->title        = $data['title'] ?? $post->title;
        $post->content      = $data['content'] ?? $post->content;

        if($post->save()) {
            $post->users()->sync($data['users'] ?? []);
            $post->groups()->sync($data['groups'] ?? []);

            return true;
        }

        return false;
    }
}
