/*
 * Solidarity Mobility API
 *
 * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 * API version: 1.0.0
 * Generated by: OpenAPI Generator (https://openapi-generator.tech)
 */

package openapi

import (
	"encoding/json"
	"net/http"
	"strings"
)

// PassengerTripRequestAPIController binds http requests to an api service and writes the service results to the http response
type PassengerTripRequestAPIController struct {
	service      PassengerTripRequestAPIServicer
	errorHandler ErrorHandler
}

// PassengerTripRequestAPIOption for how the controller is set up.
type PassengerTripRequestAPIOption func(*PassengerTripRequestAPIController)

// WithPassengerTripRequestAPIErrorHandler inject ErrorHandler into controller
func WithPassengerTripRequestAPIErrorHandler(h ErrorHandler) PassengerTripRequestAPIOption {
	return func(c *PassengerTripRequestAPIController) {
		c.errorHandler = h
	}
}

// NewPassengerTripRequestAPIController creates a default api controller
func NewPassengerTripRequestAPIController(s PassengerTripRequestAPIServicer, opts ...PassengerTripRequestAPIOption) Router {
	controller := &PassengerTripRequestAPIController{
		service:      s,
		errorHandler: DefaultErrorHandler,
	}

	for _, opt := range opts {
		opt(controller)
	}

	return controller
}

// Routes returns all the api routes for the PassengerTripRequestAPIController
func (c *PassengerTripRequestAPIController) Routes() Routes {
	return Routes{
		"PassengerPost": Route{
			strings.ToUpper("Post"),
			"/passenger",
			c.PassengerPost,
		},
	}
}

// PassengerPost - Create a Passenger Trip Request
func (c *PassengerTripRequestAPIController) PassengerPost(w http.ResponseWriter, r *http.Request) {
	authenticated := CheckOperatorAuthorization(r, AuthorizedOperators)
	if !authenticated {
		response := ImplResponse{
			Code: 401,
			Body: "Unauthorized request. Check your operator / API Key.",
		}
		EncodeJSONResponse(response.Body, &response.Code, w)
		return
	}
	passengerTripRequestParam := PassengerTripRequest{}
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&passengerTripRequestParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
	if err := AssertPassengerTripRequestRequired(passengerTripRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	if err := AssertPassengerTripRequestConstraints(passengerTripRequestParam); err != nil {
		c.errorHandler(w, r, err, nil)
		return
	}
	result, err := c.service.PassengerPost(r.Context(), passengerTripRequestParam)
	// If an error occurred, encode the error with the status code
	if err != nil {
		c.errorHandler(w, r, err, &result)
		return
	}
	// If no error, encode the body and the result code
	EncodeJSONResponse(result.Body, &result.Code, w)
}
