<?php

use Illuminate\Support\Facades\Response;

class APIResponse
{

    const SUCCESS_STATUS = 200;

    public $status; // boolean valor indicating the overall result of the request
    public $code; //The HTTP code used in the return response
    public $data; // any data to be returned in the response
    public $errors; //structure containing errors that ocurred in the request
    public $edition; // The edition that the response replies to

    public function __construct()
    {
        $this->data    = [];
        $this->errors  = [];
        $this->edition = EDP_EDITION;
    }

    /**
     * Generic function to handle a sucessfull request
     * @param array $data Any data that is returned
     * @param string $message The message to be returned, defaults to a generic success message
     */
    public static function success($data = [], $message = '')
    {

        $message       = empty($message) ? trans('api.messages.success') : $message;
        $resp          = new APIResponse();
        $resp->status  = true;
        $resp->code    = APIResponse::SUCCESS_STATUS;
        $resp->data    = $data;
        $resp->message = $message;
        $resp->edition = EDP_EDITION;
        return Response::json($resp);
    }

    /**
     * Generic function to handle a failed request
     * @param integer $code The HTTP code to be returned in the  request
     * @param string $message The message to be returned, defaults to a generic success message
     * @param array $errors Any errors that may occurr
     */
    public static function fail($code, $errors = [], $message = '')
    {

        $resp          = new APIResponse();
        $resp->status  = false;
        $resp->code    = $code;
        $resp->errors  = $errors;
        $resp->message = empty($message) ? trans('api.messages.error') : $message;
        $resp->edition = EDP_EDITION;
        return Response::json($resp, $code);
    }
}
