<?php

declare(strict_types=1);

namespace App\Repositories\Eloquent;

use App\Models\Pouzivatel\Pouzivatel;
use Spatie\Permission\Contracts\Role;
use Prettus\Repository\Eloquent\BaseRepository;
use App\Repositories\PouzivatelRepositoryContract;

/**
 * Class PouzivatelRepository
 *
 * @package App\Repositories\Eloquent
 */
final class PouzivatelRepository extends BaseRepository implements PouzivatelRepositoryContract
{
    /**
     * @return string
     */
    public function model()
    {
        return Pouzivatel::class;
    }

    /**
     * @param int    $userId
     * @param string $token
     *
     * @return void
     */
    public function saveNotificationToken(int $userId, string $token): Pouzivatel
    {
        /** @var \App\Models\Pouzivatel\Pouzivatel $user */
        $user = $this->find($userId);
        $user->setAttribute('notification_token', $token);
        $user->save();

        return $user->refresh();
    }

    /**
     * @param \Spatie\Permission\Contracts\Role $role
     *
     * @return \App\Models\Pouzivatel\Pouzivatel[]|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed|null
     */
    public function getByRole(Role $role)
    {
        $users = $this->whereHas('roles', function ($query) use ($role) {
            if ($query->where('name', 'like', $role->name)) {
                return true;
            }

            return false;
        })->get();

        return $users;
    }
}
