<?php

namespace LaraBB\Thread\Tasks;

use LaraBB\Thread\Models\Thread;

/**
 * Class UpdateThreadTask
 * @package LaraBB\Thread\Tasks
 */
class UpdateTask
{
    /**
     * @param Thread $thread
     * @param array $data
     * @return bool
     */
    public function run(Thread $thread, array $data): bool
    {
        $thread->forum_uuid     = $data['forum_uuid'] ?? $thread->forum_uuid;
        $thread->prefix_uuid    = $data['prefix_uuid'] ?? $thread->prefix_uuid;
        $thread->updated_uuid   = $data['updated_uuid'] ?? $thread->updated_uuid;
        $thread->lastpost_uuid  = $data['lastpost_uuid'] ?? $thread->lastpost_uuid;
        $thread->firstpost_uuid = $data['firstpost_uuid'] ?? $thread->firstpost_uuid;
        $thread->title          = $data['title'] ?? $thread->title;
        $thread->slug           = $data['slug'] ?? $thread->slug;
        $thread->views          = $data['views'] ?? $thread->views;
        $thread->posts          = $data['posts'] ?? $thread->posts;

        $thread->users()->sync($data['users'] ?? []);
        $thread->groups()->sync($data['groups'] ?? []);

        return $thread->save();
    }
}
