<?php

class GeofenceAreaApi extends Api
{
    public $apiName = 'geofencearea';

    /**
     * @OA\Get(
     *   path="/v1/geofencearea/api/geofencearea/countryApi",
     *   summary="Get list of countries",
     *   @OA\Response(
     *     response="200",
     *     description="return all geofence area countries",
     *     @OA\JsonContent(
     *       type="object"
     *     )
     *   )
     * )
     */
    public function countryApi()
    {
        $db = new Db();
        $result = GeofenceAreaModel::getCountryListWithoutPagination($db->getConnect());

        return $this->response($result, 200);
    }

    /**
     * @OA\Get(
     *   path="/v1/geofencearea/api/geofencearea/cityApi?country_code={country_code}",
     *   summary="Get list of cities",
     *   @OA\Response(
     *     response="200",
     *     description="city data",
     *     @OA\JsonContent(
     *       type="object"
     *     )
     *   )
     * )
     */
    public function cityApi()
    {
        $db = new Db();

        $searchParams = $this->requestParams;
        $result = GeofenceAreaModel::getCityListWithoutPagination($db->getConnect(), $searchParams);

        return $this->response($result, 200);
    }

    /**
     * @OA\Get(
     *   path="/v1/geofencearea/api/geofencearea/boundariesApi?city_id={city_id}&country_code={country_code}",
     *   summary="Get list of GPS areas by city and country_code",
     *   @OA\Response(
     *     response="200",
     *     description="area data",
     *     @OA\JsonContent(
     *       type="object"
     *     )
     *   )
     * )
     */
    public function boundariesApi()
    {
        $db = new Db();

        $searchParams = $this->requestParams;
        $result = GeofenceAreaModel::getAreaListWithoutPagination($db->getConnect(), $searchParams);

        return $this->response($result, 200);
    }

    /**
     * @OA\Get(
     *   path="/v1/geofencearea/api/geofencearea/tripsApi?from_area_id={from_area_id}&to_area_id={to_area_id}&transport_provider_id={transport_provider_id}",
     *   summary="Get list of trips between areas (filter by transport_provider_id)",
     *   @OA\Response(
     *     response="200",
     *     description="trip data",
     *     @OA\JsonContent(
     *       type="object"
     *     )
     *   )
     * )
     */
    public function tripsApi()
    {
        $db = new Db();
        $searchParams = $this->requestParams;
        $startAreaId = $searchParams['from_area_id'] ?? null;
        $endAreaId = $searchParams['to_area_id'] ?? null;

        if (!$startAreaId || !$endAreaId) {
            $result = [
                'success' => false,
                'message' => 'missing parameter values (from_area_id and to_area_id parameter are required).'
            ];
            return $this->response($result, 200);
        }

        $result = GeofenceAreaModel::getTripListBetweenArea($db->getConnect(), $startAreaId, $endAreaId, $searchParams);
        return $this->response($result, 200);
    }

    /**
     * @OA\Get(
     *   path="/v1/geofencearea/api/geofencearea/priceComparisonApi?from_area_id={from_area_id}&to_area_id={to_area_id}",
     *   summary="Compare price between areas (group by transport_provider_id)",
     *   @OA\Response(
     *     response="200",
     *     description="data",
     *     @OA\JsonContent(
     *       type="object"
     *     )
     *   )
     * )
     */
    public function priceComparisonApi()
    {
        $db = new Db();
        $searchParams = $this->requestParams;
        $startAreaId = $searchParams['from_area_id'] ?? null;
        $endAreaId = $searchParams['to_area_id'] ?? null;

        if (!$startAreaId || !$endAreaId) {
            $result = [
                'success' => false,
                'message' => 'missing parameter values (from_area_id and to_area_id parameter are required).'
            ];
            return $this->response($result, 200);
        }

        $data = GeofenceAreaModel::priceComparison($db->getConnect(), $startAreaId, $endAreaId);

        $result = [
            'success' => true,
            'data' => $data ?: []
        ];
        return $this->response($result, 200);
    }

    /**
     * @OA\Get(
     *   path="/v1/geofencearea/api/geofencearea/priceComparisonFromAreasToOneDestinationApi?from_area_id_list=3,4,5,2,7&to_area_id=9",
     *   summary="Compare price from list of areas to one destination (group by transport_provider_id)",
     *   @OA\Response(
     *     response="200",
     *     description="data",
     *     @OA\JsonContent(
     *       type="object"
     *     )
     *   )
     * )
     */
    public function priceComparisonFromAreasToOneDestinationApi()
    {
        $db = new Db();
        $searchParams = $this->requestParams;

        // with params format fo ....&from_area_id_list=2,3,4,5,8,9&to_area_id=15
        $startAreaIdList = $searchParams['from_area_id_list'] ?? null;
        $endAreaId = $searchParams['to_area_id'] ?? null;

        if (!$startAreaIdList || !$endAreaId) {
            $result = [
                'success' => false,
                'message' => 'missing parameter values (from_area_id_list and to_area_id parameter are required).'
            ];
            return $this->response($result, 200);
        }

        // convert from_area_id_list=2,3,4,5,8,9 => [2,3,4,5,8,9]
        $startAreaIdList = explode(',', $startAreaIdList);

        $data = [];
        foreach ($startAreaIdList as $startAreaId) {
            $data[] = GeofenceAreaModel::priceComparison($db->getConnect(), $startAreaId, $endAreaId);
        }

        $result = [
            'success' => true,
            'data' => $data ?: []
        ];
        return $this->response($result, 200);
    }

    public function indexAction()
    {

    }

    public function viewAction()
    {

    }

    public function createAction()
    {

    }

    public function updateAction()
    {

    }

    public function deleteAction()
    {

    }

    protected function getAction()
    {
        $method = $this->method;
        switch ($method) {
            case 'GET':
                $pos = strpos($this->requestUri[0], '?');
                if ($pos !== false && $pos == 0) {
                    // We want to get "all"
                    $this->requestUri[0] = "all" . $this->requestUri[0];
                }
                $tok = strtok($this->requestUri[0], '?');

                if ($tok === false || $tok == 'all') {
                    return 'indexAction';
                } else {
                    return $tok;
                }
                break;
            case 'POST':
                return 'createAction';
                break;
            case 'PUT':
                return 'updateAction';
                break;
            case 'DELETE':
                return 'deleteAction';
                break;
            default:
                return null;
        }
    }
}
