<?php

class RouteApi extends Api
{
    public $apiName = 'route';

    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"] ?? [];
        $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);

            $option["member_id"] = $member_id;
            $trip["member_id"] = $member_id;
            list ($res, $trip, $route) = Route::getTransportRoute($db->getConnect(), $origin, $destination, $option, $trip);
            if (is_array($res)) {
                return $this->response([
                    'option' => $res,
                    'trip' => $trip,
                    'route' => $route
                ], 200);
            } else {
                throw new Exception("Failed to get transport route 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);
    }
}
