<?php

namespace App\Services;

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

class PermissionService
{
    public function getAll(): Collection
    {
        return Permission::query()->orderBy('created_at', 'desc')->get();
    }

    public function findById(int $id): ?Permission
    {
        return Permission::find($id);
    }

    public function findByScope(string $scope): ?Permission
    {
        return Permission::where('scope', $scope)->first();
    }

    public function create(array $data): Permission
    {
        return Permission::create($data);
    }

    public function update(int $id, array $data): ?Permission
    {
        $model = $this->findById($id);

        if (! $model) {
            return null;
        }

        $model->update($data);

        return $model->fresh();
    }

    public function delete(int $id): bool
    {
        $model = $this->findById($id);

        if (! $model) {
            return false;
        }

        return $model->delete();
    }

    // Add custom business logic methods here
}
