<?php

namespace LaraBB\Forum\Tasks\Permission;

use Illuminate\Database\Eloquent\Collection;
use LaraBB\Forum\Models\Permission;

/**
 * Class UpdateForumPermissionsTask
 * @package LaraBB\Forum\Tasks
 */
class UpdateTask
{
    /**
     * @param Collection $permissions
     * @param $data
     * @return bool
     */
    public function run(Collection $permissions, $data): bool
    {
        if(empty($data)) return true;

        $permissions->map(function (Permission $permission) use ($data) {
            $permission->updated_uuid   = $data[$permission->uuid]['updated_uuid'] ?? $permission->updated_uuid;
            $permission->show_forum     = $data[$permission->uuid]['show_forum'] ?? $permission->show_forum;
            $permission->show_threads   = $data[$permission->uuid]['show_threads'] ?? $permission->show_threads;
            $permission->show_posts     = $data[$permission->uuid]['show_posts'] ?? $permission->show_posts;
            $permission->create_threads = $data[$permission->uuid]['create_threads'] ?? $permission->create_threads;
            $permission->create_posts   = $data[$permission->uuid]['create_posts'] ?? $permission->create_posts;
            $permission->edit_threads   = $data[$permission->uuid]['edit_threads'] ?? $permission->edit_threads;
            $permission->edit_posts     = $data[$permission->uuid]['edit_posts'] ?? $permission->edit_posts;
            $permission->delete_threads = $data[$permission->uuid]['delete_threads'] ?? $permission->delete_threads;
            $permission->delete_posts   = $data[$permission->uuid]['delete_posts'] ?? $permission->delete_posts;
            $permission->save();
        });

        return true;
    }
}
