<?php

declare(strict_types=1);

namespace App\Repositories\Eloquent;

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

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

    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();
    }

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

            return false;
        })->get();

        return $users;
    }
}
