<?php
namespace App\Services\Response;

use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * Class based Helper to upload and resize Images
 */
class ResponseService
{
    public static function fetchPainateDate(array $params = [])
    {
        $request = $params['request'];
        $query = $params['query'];
        $per_page = $request->input('per_page');
        $page = $request->input('page');
        $resourceClass = $params['resourceClass'];
        $returnName = $params['returnName'];
        $responseData = [];
        if ($per_page && $page) {
            $paginator = $query->paginate($per_page)->appends($request->query());
            $items = $paginator->items(); //array
            $responseData = [
                "pagination" => [
                    'current_page' => $paginator->currentPage(),
                    'next_page' => $paginator->lastPage() > $paginator->currentPage() ? $paginator->currentPage() + 1 : null,
                    'last_page' => $paginator->lastPage(),
                    'total'=>$paginator->total(),
                    'per_page'=>$paginator->perPage()
                ],
                $returnName => $resourceClass::collection($items),
            ];
        } else {
            $entities = $query->get();
            $responseData = [
                "pagination" => null,
                $returnName => $resourceClass::collection($entities),
            ];
        }
        throw new HttpResponseException(response()->json($responseData, 200));
    }
    public static function response($response)
    {
        if ($response['status'] == 200) {
            return response()->json
            (
                [
                    'message' => $response['message'],
                    'data' => $response['data']
                ]
                ,
                $response['status']
            );
        }
        throw new HttpResponseException(response()->json(['errors' => $response['message'], 'message' => $response['message']], $response['status']));
    }

    public static function notFound()
    {
        $status = 400;
        $message = __('web.not_found');
        throw new HttpResponseException(response()->json(['errors' => $message], $status));
    }

    public static function responseFail($error)
    {
        $status = 400;
        $message = $error;
        throw new HttpResponseException(response()->json(['errors' => $message], $status));
    }
}


