<?php

namespace App\Support;

use Illuminate\Http\JsonResponse;

class ApiResponse
{
    public static function success(string $message = 'Success', mixed $data = null, int $code = 200): JsonResponse
    {
        return response()->json([
            'status' => 'success',
            'message' => $message,
            'data' => $data,
        ], $code);
    }

    public static function error(string $message = 'Error', mixed $data = null, int $code = 400): JsonResponse
    {
        return response()->json([
            'status' => 'error',
            'message' => $message,
            'data' => $data,
        ], $code);
    }

    public static function paginated(string $message, $paginator, mixed $extra = []): JsonResponse
    {
        $items = $paginator->items();
        return self::success($message, array_merge([
            // Frontend expects `documents`; keep `items` as an alias for callers
            // that prefer the generic name.
            'documents' => $items,
            'items' => $items,
            'pagination' => [
                'currentPage' => $paginator->currentPage(),
                'totalPages' => $paginator->lastPage(),
                'totalRecords' => $paginator->total(),
                'pageSize' => $paginator->perPage(),
            ],
        ], (array) $extra));
    }
}
