<?php

namespace LaraBB\Post\Tasks;

use LaraBB\Post\Models\Post;

/**
 * Class StorePostTask
 * @package LaraBB\Post\Tasks
 */
class StoreTask
{
    /**
     * StorePostTask constructor.
     * @param Post $post
     */
    public function __construct(private readonly Post $post)
    {
    }

    /**
     * @param $data
     * @return bool|Post
     */
    public function run($data): Post|bool
    {
        $this->post->created_uuid = $data['created_uuid'];
        $this->post->updated_uuid = $data['updated_uuid'];
        $this->post->thread_uuid  = $data['thread_uuid'];
        $this->post->title        = $data['title'];
        $this->post->content      = $data['content'];

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

            return $this->post;
        }

        return false;
    }
}
