<?php

namespace App\Helpers;

class ApiResponse
{
    public static function success(
        string $message = '',
        $data = [],
        int $code = 200,
        array $pagination = []
    ) {
        return response()->json([
            'success' => true,
            'message' => $message,
            'data' => $data,
            'pagination' => (object) $pagination
        ], $code);
    }

    public static function error(
        string $message = '',
        $errors = [],
        int $code = 400
    ) {
        return response()->json([
            'success' => false,
            'message' => $message,
            'errors' => $errors
        ], $code);
    }
    
    public static function pagination($paginator, $resourceClass, $message = 'Data fetched successfully.')
    {
        return response()->json([
            'success' => true,
            'message' => $message,
            'data' => $resourceClass::collection($paginator->getCollection()),
            'pagination' => [
                'current_page' => $paginator->currentPage(),
                'last_page' => $paginator->lastPage(),
                'per_page' => $paginator->perPage(),
                'total' => $paginator->total(),
                'from' => $paginator->firstItem(),
                'to' => $paginator->lastItem(),
                'next_page_url' => $paginator->nextPageUrl(),
                'prev_page_url' => $paginator->previousPageUrl(),
            ],
        ]);
    }
}