package handlers

import (
	"errors"
	"fmt"
	"net/http"

	"otaupdate/services"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/logger"
	"github.com/fiskerinc/cloud-services/pkg/redis"
	"github.com/fiskerinc/cloud-services/pkg/utils"
	"github.com/fiskerinc/cloud-services/pkg/utils/querystring"
	"github.com/go-pg/pg/v10"
	"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"
)

// HandleCarUpdatesStatuses godoc
// @Summary Gets statuses for car update by car update ids
// @Description Returns array of car update statuses
// @Accept  json
// @Produce  json
// @Param Authorization header string false "Bearer <ID token>"
// @Param Api-Key header string false "<API token>"
// @Param carupdateids query string true "Comma delimited list of car update ids"
// @Success 200 {object} CarUpdateStatuses "Car update statuses"
// @Failure 400 {object} common.JSONError "Bad request"
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /carupdatesstatuses [get]
func HandleCarUpdatesStatuses(w http.ResponseWriter, r *http.Request) {
	data, err := validateCarUpdatesStatuses(r)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusBadRequest) {
		return
	}

	keys := make([]string, len(data))
	// These should be of type common.CarUpdateProgress
	statuses := make([]interface{}, len(data))
	for i, carupdateID := range data {
		keys[i] = redis.CarUpdateStatusHashKey(carupdateID)
		statuses[i] = &common.CarUpdateProgress{}
	}

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

	err = conn.GetObjectsMulti(keys, statuses)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable) {
		return
	}

	utils.RespJSON(w, http.StatusOK, CarUpdateStatuses{
		Statuses: statuses,
	})
}

// I am not a fan of doing this, going from Redis and then checking the database
func filterCarUpdatedStatuses(statuses []interface{}) (filtered []interface{}) {
	filtered = make([]interface{}, 0)
	for x := range statuses {
		status, ok := statuses[x].(common.CarUpdateProgress)
		if !ok {
			continue
		}

		// LOAD does not search on CarUpdateID
		manifest := &common.CarUpdate{ID: status.CarUpdateID, UpdateManifest: &common.UpdateManifest{ManifestType: common.MagnaManifestUpdateType}}
		um := services.GetDB().GetCarUpdates()

		err := um.Load(manifest)
		if err != nil {
			if errors.Is(err, pg.ErrNoRows) {
				continue
			}
			logger.Warn().Err(err).Send()
		}
		if manifest.ID > 0 {
			filtered = append(filtered, statuses[x])
		}
	}

	return filtered
}

func validateCarUpdatesStatuses(r *http.Request) ([]int64, error) {
	qs := r.URL.Query()
	qsIDs := qs.Get("carupdateids")

	if qsIDs == "" {
		return nil, fmt.Errorf("car update ids required")
	}

	if len(qsIDs) > 6000 {
		return nil, fmt.Errorf("carupdateids too long")
	}

	carupdateIDs, err := querystring.SplitIntArray(qsIDs)
	if err != nil {
		return nil, err
	}

	return carupdateIDs, nil
}

type CarUpdateStatuses struct {
	Statuses []interface{} `json:"statuses"`
}
