package handlers

import (
	"net/http"
	"otaupdate/services"
	"strconv"

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

// HandleDigitalTwinSignal godoc
// @Summary List signals with updated timestamp
// @Description Returns list of state with last updated timestamp.
// @Accept  json
// @Produce  json
// @Param Api-Key header string false "<API token>"
// @Param limit query int false "Limit"
// @Param offset query int false "Offset"
// @Success 200 {object} []interface{}
// @Failure 400 {object} common.JSONError "Bad request"
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /ditto/carstate [get]
func HandleDigitalTwinSignal(w http.ResponseWriter, r *http.Request) {
	limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
	offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))

	if limit < 1 || limit > 1000 {
		limit = 1000
	}

	if offset < 1 {
		offset = 0
	}

	// vin := r.URL.Query().Get("vin")
	// if vin != "" {
	// 	vins := strings.Split(vin, ",")
	// 	for _, v := range vins {
	// 		ok, err := validator.ValidateVINSimple(v)
	// 		if !ok || err != nil {
	// 			loggerdataresp.BadDataErrorResp(w, ErrInvalidVIN, http.StatusBadRequest)
	// 			return
	// 		}
	// 	}
	// }

	conn := services.RedisClientPool().GetFromPool()
	defer conn.Close()

	data := cache.NewDigitalTwinTimestampState(conn).GetDigitalTwinSignals(offset, limit)
	utils.RespJSON(w, http.StatusOK, data)
}
