<?php

namespace App\Services;

use App\Models\User;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class UserService
{

    public function __construct(protected User $user) {}
    public function createUser(array $user)
    {
        $permissions = [$user['permissions']];
        $user['permissions'] = $permissions;
        return User::create($user);
    }

    public function showUsers()
    {
        $users = User::all();
        return $users;
    }

    public function updateUser(User $user, $data)
    {
        $data['permissions'] = [$data['permissions']];
        return $user->update($data);
    }

    public function deleteUser($user)
    {
        if ($user->id === Auth::user()->id) {
            throw new Exception('Não é possível excluir seu próprio usuário.', 403);
        }

        $user->delete();
        return $user;
    }
}
