/*
 * 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 (
	"net/http"
	"strings"
)

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

// BookingsFilterAPIOption for how the controller is set up.
type BookingsFilterAPIOption func(*BookingsFilterAPIController)

// WithBookingsFilterAPIErrorHandler inject ErrorHandler into controller
func WithBookingsFilterAPIErrorHandler(h ErrorHandler) BookingsFilterAPIOption {
	return func(c *BookingsFilterAPIController) {
		c.errorHandler = h
	}
}

// NewBookingsFilterAPIController creates a default api controller
func NewBookingsFilterAPIController(s BookingsFilterAPIServicer, opts ...BookingsFilterAPIOption) Router {
	controller := &BookingsFilterAPIController{
		service:      s,
		errorHandler: DefaultErrorHandler,
	}

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

	return controller
}

// Routes returns all the api routes for the BookingsFilterAPIController
func (c *BookingsFilterAPIController) Routes() Routes {
	return Routes{
		"FilterBookingsByStatus": Route{
			strings.ToUpper("Get"),
			"/bookings",
			c.FilterBookingsByStatus,
		},
	}
}

// FilterBookingsByStatus - Filter and retrieve bookings by status, type, and ID.
func (c *BookingsFilterAPIController) FilterBookingsByStatus(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
	}
	query := r.URL.Query()
	operatorParam := query.Get("operator")
	statusParam := query.Get("status")
	filterTypeParam := query.Get("filterType")
	idParam := query.Get("Id")
	result, err := c.service.FilterBookingsByStatus(r.Context(), operatorParam, statusParam, filterTypeParam, idParam)
	// 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)
}
