package handlers

import (
	"net/http"

	"otaupdate/services"

	"github.com/fiskerinc/cloud-services/pkg/utils"
	"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"
)

// HandleVehicleYears godoc
// @Summary Returns vehicle years
// @Description Returns vehicle years
// @Accept  json
// @Produce  json
// @Param Authorization header string false "Bearer <ID token>"
// @Param Api-Key header string false "<API token>"
// @Success 200 {object} JSONYearsResult
// @Failure 400 {object} common.JSONError "Bad request"
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /vehicleyears [get]
func HandleVehicleYears(w http.ResponseWriter, r *http.Request) {
	c := services.GetDB().GetCars()

	years, err := c.GetYears()
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable, loggerdataresp.PostgresNoRowsErrorCheck) {
		return
	}

	utils.RespJSON(w, http.StatusOK, JSONYearsResult{
		Data: years,
	})
}

type JSONYearsResult struct {
	Data []int `json:"data"`
}
