<?php

namespace LaraBB\Thread\Tasks;

use LaraBB\Thread\Models\Thread;

/**
 * Class StoreThreadTask
 * @package LaraBB\Thread\Tasks
 */
class StoreTask
{
    /**
     * StoreThreadTask constructor.
     * @param Thread $thread
     */
    public function __construct(private readonly Thread $thread)
    {

    }

    /**
     * @param $data
     * @return false|Thread
     */
    public function run($data)
    {
        $this->thread->created_uuid = $data['created_uuid'];
        $this->thread->updated_uuid = $data['updated_uuid'];
        $this->thread->forum_uuid   = $data['forum_uuid'];
        $this->thread->prefix_uuid  = $data['prefix_uuid'];
        $this->thread->title        = $data['title'];
        $this->thread->slug         = $data['slug'];
        $this->thread->views        = 0;
        $this->thread->posts        = 1;

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

            return $this->thread;
        }

        return false;
    }
}
