<?php

namespace LaraBB\User\Tasks;

use LaraBB\User\Models\User;

/**
 * Class UpdateUserTask
 * @package LaraBB\User\Tasks
 */
class UpdateTask
{
    /**
     * @param User $user
     * @param array $data
     * @return bool
     */
    public function run(User $user, array $data): bool
    {
        $user->updated_uuid     = $data['updated_uuid'] ?? $user->updated_uuid;
        $user->deactivated_at   = $data['deactivated_at'] ?? $user->deactivated_at;
        $user->deactivated_uuid = $data['deactivated_uuid'] ?? $user->deactivated_uuid;
        $user->username         = $data['username'] ?? $user->username;
        $user->email_hashed     = $data['email_hashed'] ?? $user->email_hashed;
        $user->email_encrypted  = $data['email_encrypted'] ?? $user->email_encrypted;
        $user->password         = $data['password'] ?? $user->password;

        if(isset($data['groups'])) {
            $user->groups()->sync($data['groups']);
        }

        return $user->save();
    }
}
