<?php

namespace App\ThirdParty\Biteship;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\ValidationException;

class Rate
{

    public function byLatLong($params)
    {
        $origin_latitude = $params["origin_latitude"];
        $origin_longitude = $params["origin_longitude"];
        $destination_latitude = $params["destination_latitude"];
        $destination_longitude = $params["destination_longitude"];
        $items = $params["items"];
        $sha1 = sha1(json_encode($items));


        $key = implode("_", [$origin_latitude, $origin_longitude,  $destination_latitude, $destination_longitude, $sha1]);
        return Cache::remember("rates_".$key, 60 * 60 * 24, function()
        use ($origin_latitude,
            $origin_longitude,
            $destination_latitude,
            $destination_longitude,
            $items) {
            $url = env("BITESHIP_URL");
            $key = env("BITESHIP_KEY");

            $body = [
                    "origin_latitude" => $origin_latitude,
                    "origin_longitude" => $origin_longitude,
                    "destination_latitude" => $destination_latitude,
                    "destination_longitude" => $destination_longitude,
                    "couriers" => env("BITESHIP_COURIER","grab,gojek,tiki,jnt,anteraja"),
                    "items" => $items
            ];
            $res = Http::withHeaders([
                    "authorization" => $key
                ])
                ->withBody(json_encode($body), 'application/json')
                ->post($url."/v1/rates/couriers");

                if ($res->status() != 200 && $res->json()['error'] != null) {
                    Log::info(json_encode($res->json()));
                    Log::info($body);
                    throw ValidationException::withMessages([
                        "message" => $res->json()['error']
                    ]);
                }

            if ($res->status() == 200)
                return $res->json();

            return null;
        });
    }

    public function byPostal($params)
    {
        $destination_postal_code = $params["destination_postal_code"];
        $origin_postal_code = $params["origin_postal_code"];
        $items = $params["items"];
        $sha1 = sha1(json_encode($items));

        $key = $origin_postal_code."_".$destination_postal_code."_".$sha1;
        return Cache::remember("rates_".$key, 60 * 60 * 24, function() use (
            $origin_postal_code,
            $destination_postal_code,
            $items) {
            $url = env("BITESHIP_URL");
            $key = env("BITESHIP_KEY");
            $res = Http::withHeaders([
                    "authorization" => $key
                ])
                ->withBody(json_encode([
                    "origin_postal_code" => $origin_postal_code,
                    "destination_postal_code" => $destination_postal_code,
                    "couriers" => env("BITESHIP_COURIER","tiki,jnt,anteraja"),
                    "items" => $items
                ]), 'application/json')
                ->post($url."/v1/rates/couriers");

                 if ($res->status() != 200 && $res->json()['error'] != null) {
                    throw ValidationException::withMessages([
                        "message" => $res->json()['error']
                    ]);
                }
                
            if ($res->status() == 200)
                return $res->json();

            return null;
        });
    }
}
