<?php

namespace App\Repositories;

use App\Models\Role;
use Illuminate\Database\Eloquent\Collection;

class RoleRepository
{
    protected $role;

    public function __construct(Role $role)
    {
        $this->role = $role;
    }

    public function getAll(): Collection
    {
        return $this->role->all();
    }

    public function findById(int $id): ?Role
    {
        return $this->role->find($id);
    }

    public function findByNama(string $nama): ?Role
    {
        return $this->role->where('nama', $nama)->first();
    }

    public function create(array $data): Role
    {
        return $this->role->create($data);
    }

    public function update(int $id, array $data): ?Role
    {
        $role = $this->findById($id);
        if ($role) {
            $role->update($data);
        }
        return $role;
    }

    public function delete(string $id): bool
    {
        return $this->findById($id)->delete();
    }
}
