<?php

namespace App\ThirdParty\Biteship;

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

class Tracking
{

    public function byWaybill($params)
    {
        $waybill_id = $params["waybill_id"];
        $courier = $params["courier"];

        $key = $waybill_id ." ". $courier;
        return Cache::remember("tracking_waybill_".$key, 60 * 60 * 24, function() use ($waybill_id, $courier) {
            $url = env("BITESHIP_URL");
            $key = env("BITESHIP_KEY");
            $res = Http::withHeaders([
                    "authorization" => $key
                ])
                ->get($url."/v1/trackings/".$waybill_id."/couriers/".$courier);

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

            return null;
        });
    }


    public function byId($params)
    {
        $id = $params["id"];

        $key = $id ;
        return Cache::remember("tracking_id_".$key, 60 * 60 * 24, function() use ($id) {
            $url = env("BITESHIP_URL");
            $key = env("BITESHIP_KEY");
            $res = Http::withHeaders([
                    "authorization" => $key
                ])
                ->get($url."/v1/trackings/".$id);

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

            return null;
        });
    }

}
