<?php

namespace LaraBB\Settings\Tasks\Prefix;

use LaraBB\Settings\Models\Prefix;

/**
 * Class StorePrefixTask
 * @package LaraBB\Settings\Tasks\Prefix
 */
class StoreTask
{
    /**
     * StorePrefixTask constructor.
     * @param Prefix $prefix
     */
    public function __construct(private readonly Prefix $prefix)
    {

    }

    /**
     * @param array $data
     * @return bool
     */
    public function run(array $data): bool
    {
        $this->prefix->created_uuid  = $data['created_uuid'];
        $this->prefix->updated_uuid  = $data['updated_uuid'];
        $this->prefix->prefix        = $data['prefix'];
        $this->prefix->display_style = $data['display_style'];

        if($this->prefix->save()) {
            $this->prefix->forums()->syncWithoutDetaching($data['forums'] ?? []);

            return true;
        }

        return false;
    }
}
