<?php

namespace App\Functions;

use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Route;
use Modules\Cart\Entities\Model as Cart;

class ResponseHelper
{
    public static function make($data, $msg = '', $success = true, $statusCode = 200)
    {
        $response = [
            'msg' => $msg,
            'statusCode' => $statusCode,
            'success' => $success,
        ];

        $app_lock = false;
        if (version_compare(request('device_version'), setting('android_lock_version'), '<') && request('device_type') == 'android') {
            $app_lock = true;
        } elseif (version_compare(request('device_version'), setting('ios_lock_version'), '<') && request('device_type') == 'ios') {
            $app_lock = true;
        }

        if ($_SERVER['REQUEST_METHOD'] == 'GET') {
            $response['cart_count'] = Cart::where('client_id', auth('sanctum')->id())->count();
        }

        $response['payload'] = $data;
        
        if (Route::currentRouteName() == 'api.home') {
            $response['app_lock'] = $app_lock;
            $response['ios_version'] = setting('ios_version');
            $response['android_version'] = setting('android_version');
            $response['ios_link'] = setting('apple');
            $response['android_link'] = setting('android');
            $response['app_lock_msg'] = __('trans.app_lock_msg');
        }
        throw new HttpResponseException(response()->json($response, $statusCode));
    }
    
    
}
