<?php

class OptionsApi extends Api
{
    public $apiName = 'options';

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

        $latitude = $this->requestParams["latitude"] ?? 0;
        $longitude = $this->requestParams["longitude"] ?? 0;

        try {
            if ($latitude==null || $longitude==null || ($latitude==0 || $longitude==0)) {
                throw new Exception('Invalid location: '.$latitude.",".$longitude);
            }
            $db = new Db();

            list ($res, $details) = Options::getServicesByLatitudeLongitude($db->getConnect(), $latitude, $longitude);
            if (is_array($res)) {
                return $this->response([
                    'services' => $res,
                    'details' => $details
                ], 200);
            } else {
                throw new Exception("Failed to get transport options for origin");
            }
        } catch (Exception $e) {
            $message = $e->getMessage();
        }
        return $this->response(
            array(
                "error" => $message
            ), 500);
    }

    /**
     * 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"] ?? [];
        $create = $this->requestParams["create"] ?? false;
        $country = $this->requestParams["country"] ?? NULL;
        $duration = $this->requestParams["duration"] ?? 0;
        $distance = $this->requestParams["distance"] ?? 0;
        $member_id = $this->requestParams["member_id"] ?? 0;

        try {
            $db = new Db();
            
            list ($origin, $destination) = Options::checkValidLocations($db->getConnect(), $origin, $destination);

            list ($res, $details) = Options::getTransportOptions($db->getConnect(), $origin, $destination);
            if (is_array($res)) {
                $trip = NULL;
                if ($create) {
                    $country = $origin["country"];
                    $trip = Options::createTrip($db, $country, $duration, $distance, $origin["id"], $destination["id"]);
                }
                return $this->response([
                    'services' => $res,
                    'details' => $details,
                    'trip' => $trip
                ], 200);
            } else {
                throw new Exception("Failed to get transport options 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);
    }

}
