<?php

namespace LaraBB\User\Tasks;

use LaraBB\User\Models\User;

/**
 * Class StoreUserTask
 * @package LaraBB\User\Tasks
 */
class StoreTask
{
    /**
     * StoreUserTask constructor.
     * @param User $user
     */
    public function __construct(private readonly User $user)
    {

    }

    /**
     * @param array $data
     * @return User
     */
    public function run(array $data): User
    {
        $this->user->pid                     = $data['pid'];
        $this->user->created_uuid            = $data['created_uuid'];
        $this->user->updated_uuid            = $data['updated_uuid'];
        $this->user->deactivated_at          = $data['deactivated_at'];
        $this->user->deactivated_uuid        = $data['deactivated_uuid'];
        $this->user->registered_at           = $data['registered_at'];
        $this->user->registered_ip           = $data['registered_ip'];
        $this->user->username                = $data['username'];
        $this->user->email_hashed            = $data['email_hashed'];
        $this->user->email_encrypted         = $data['email_encrypted'];
        $this->user->password                = $data['password'];
        $this->user->passwordchange_required = $data['passwordchange_required'];

        $this->user->save();
        $this->user->groups()->attach($data['groups']);


        return $this->user;
    }
}
