<?php

namespace App\Filament\Resources\ProjectCharterChangeRequestResource\Api\Handlers;

use App\Filament\Resources\ProjectCharterChangeRequestResource;
use App\Filament\Resources\ProjectCharterChangeRequestResource\Api\Transformers\ProjectCharterChangeRequestTransformer;
use Illuminate\Http\Request;
use Rupadana\ApiService\Http\Handlers;
use Spatie\QueryBuilder\QueryBuilder;

class PaginationHandler extends Handlers
{
    public static ?string $uri = '/';

    public static ?string $resource = ProjectCharterChangeRequestResource::class;

    /**
     * List of ProjectCharterChangeRequest
     *
     * @param  Request  $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function handler()
    {
        $query = static::getEloquentQuery();

        $query = QueryBuilder::for($query)
            ->allowedFields($this->getAllowedFields() ?? [])
            ->allowedSorts($this->getAllowedSorts() ?? [])
            ->allowedFilters($this->getAllowedFilters() ?? [])
            ->allowedIncludes($this->getAllowedIncludes() ?? [])
            ->paginate(request()->query('per_page'))
            ->appends(request()->query());

        return ProjectCharterChangeRequestTransformer::collection($query);
    }
}
