<?php

namespace App\Http\Controllers\PermitsReminder;

use App\Models\PermitsReminder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ListPermitsReminderController
{
    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $perPage = 10; // Number of items per page
        $page = request('page', 1); // Get the current page from the request

        // Paginate the PermitsReminder model
        $query = PermitsReminder::orderBy('id', 'desc');
        $paginator = $query->paginate($perPage, ['*'], 'page', $page);

        // Convert paginated data to an array
        $data = $paginator->items();

        // Create pagination links
        $links = [
            "first" => $paginator->url(1),
            "last" => $paginator->url($paginator->lastPage()),
            "prev" => $paginator->previousPageUrl(),
            "next" => $paginator->nextPageUrl(),
        ];

        $response = [
            "title" => "List Permits Reminder",
            "message" => "You have successfully retrieved a list of Permits Reminder",
            "payload" => [
                'data' => $data,
                "links" => $links,
                "meta" => [
                    "current_page" => $paginator->currentPage(),
                    "from" => $paginator->firstItem(),
                    "last_page" => $paginator->lastPage(),
                    "path" => $request->path(),
                    "per_page" => $paginator->perPage(),
                    "to" => $paginator->lastItem(),
                    "total" => $paginator->total(),
                ]
            ]
        ];

        return response()->json($response);
    }
}
