<?php

class QuoteApi extends Api
{
    public $apiName = 'quote';

    protected $hasPrice = true;

    public function __construct($requestUri, $encryption=true) {
      $this->cacheWhitelist = [
        "createAction" => ['ttl' => 300] // 300 sec. = 5 min.
      ];
      parent::__construct($requestUri, $encryption);
    }


    public function indexAction()
    {
        return $this->response(
            array(
                'error' => 'Data not found'
            ), 404);
    }

    /**
     * Method GET
     * Get single record (by id)
     * http://DOMAIN/remove/1
     * @return string
     */
    public function viewAction()
    {
        //id must be the first parameter after /remove/x
        return $this->response(
            array(
                'error'=> 'Data not found'
            ), 404);
    }

    public function createAction()
    {
        $message = "Unexpected options error";

        $origin = $this->requestParams["origin"] ?? [];
        $destination = $this->requestParams["destination"] ?? [];
        $option = $this->requestParams["option"] ?? [];
        $trip = $this->requestParams["trip"] ?? [];
        $route = $this->requestParams["route"] ?? [];
        $member_id = $this->requestParams["member_id"] ?? 0;

        try {
            $db = new Db();
            
            list ($origin, $destination) = Options::checkValidLocations($db->getConnect(), $origin, $destination);
            Options::checkValidOption($db->getConnect(), $option);
            Options::checkValidTrip($db->getConnect(), $trip);
            Route::checkValidRoute($db->getConnect(), $route);

            $option["member_id"] = $member_id;
            $trip["member_id"] = $member_id;
            list ($option, $trip, $quote) = Quote::getTransportQuote($db->getConnect(), $origin, $destination, $option, $trip, $route);
            if (is_array($quote)) {
                $this->hasPrice = array_key_exists('cost', $quote) && isset($quote['cost']) && intval($quote['cost']) > 0;
                return $this->response([
                    'option' => $option,
                    'trip' => $trip,
                    'quote' => $quote,
                    'route' => $route
                ], 200);
            } else {
                throw new Exception("Failed to get transport quote for origin");
            }
        } catch (Exception $e) {
            $message = $e->getMessage();
        }
        return $this->response(
            array(
                "error" => $message
            ), 500);
    }

    public function updateAction()
    {
        return $this->response(
            array(
                "error" => "Update error"
            ), 400);
    }

    public function deleteAction()
    {
        return $this->response(
            array(
                "error" => "Delete error"
            ), 500);
    }

    protected  function storeInCache() {
      return http_response_code() === 200 && $this->hasPrice;
    }

    protected  function getCacheKey() {
      $origin = json_encode($this->requestParams["origin"]);
      $destination = json_encode($this->requestParams["destination"]);
      $option = $this->requestParams["option"];
      $transport_provider_id = $option["transport_provider_id"];
      return hash('md5', $this->method.'|'.$this->apiName.'|'.$this->action.'|'.$origin.'|'.$destination.'|'.$transport_provider_id);
    }

}
