package handlers

import (
	"encoding/json"
	"fmt"
	"net/http"
	"otaupdate/services"
	"strconv"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/common/actionlogger"
	"github.com/fiskerinc/cloud-services/pkg/common/carupdatestatus"
	"github.com/fiskerinc/cloud-services/pkg/common/handlers"
	"github.com/fiskerinc/cloud-services/pkg/httphandlers"
	"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/validator"

	"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"
	re "github.com/gomodule/redigo/redis"
	"github.com/julienschmidt/httprouter"
)

// HandleCarUpdateDeploy godoc
// @Summary Deploy car update
// @Description Deploys car update and send notifications
// @Accept  json
// @Produce  json
// @Param Authorization header string false "Bearer <ID token>"
// @Param Api-Key header string false "<API token>"
// @Param id path string true "Car update id to deploy"
// @Success 200 {object} common.JSONMessage "Request result"
// @Failure 400 {object} common.JSONError "Bad request"
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /carupdate/{id}/deploy [post]
func HandleCarUpdateDeploy(w http.ResponseWriter, r *http.Request) {
	params := httprouter.ParamsFromContext(r.Context())
	id, err := strconv.ParseInt(params.ByName("id"), 10, 64)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusBadRequest) {
		return
	}

	req := common.CarUpdateRequest{
		CarUpdateID: id,
	}

	err = validator.ValidateStruct(req)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusBadRequest) {
		return
	}

	carupdates := services.GetDB().GetCarUpdates()
	cu, err := carupdates.SelectByID(req.CarUpdateID)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable, loggerdataresp.PostgresNoRowsErrorCheck) {
		return
	}

	alDB := services.GetDB().GetActionLog()
	go func() {
		actionLog := actionlogger.ActionLog{
			VIN:            cu.VIN,
			Action:         actionlogger.CarUpdate,
			UserIdentifier: httphandlers.GetClientID(r),
			CallLocation:   "github.com/fiskerinc/cloud-services/services/ota_update_go/handlers/carupdate_deploy.go",
			Description:    fmt.Sprintf("car update id: %d", req.CarUpdateID),
		}

		err = alDB.Insert(actionLog)
		if err != nil {
			logger.Err(err).Msg("failed to insert action log inside HandleCarUpdateDeploy")
		}
	}()

	if !isRedeployAvailable(cu.Status) {
		utils.RespJSON(w, http.StatusUnprocessableEntity, common.JSONMessage{
			Message: fmt.Sprintf("Unable to redeploy, CarUpdate is currently %s", cu.Status),
		})
		return
	}

	current := common.CarUpdate{
		ID:     req.CarUpdateID,
		Status: carupdatestatus.Pending,
	}
	_, err = carupdates.UpdateStatus(&current)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable) {
		return
	}

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

	key := redis.CarUpdateStatusHashKey(req.CarUpdateID)
	msg, err := json.Marshal(common.Message{
		Handler: handlers.UpdateManifestInstall,
		Data:    req,
	})
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable) {
		return
	}

	batch := redis.NewRedisBatchCommands()
	batch.Add(re.Args{}.Add("HSET").Add(key).AddFlat(common.CarUpdateProgress{
		CarUpdateID: req.CarUpdateID,
		Status:      carupdatestatus.Pending,
	})...)
	batch.Add("EXPIRE", key, 3600)
	batch.Add("RPUSH", redis.QueueKey(common.TRex.Key(cu.VIN)), msg)
	batch.Add("EXPIRE", redis.QueueKey(common.TRex.Key(cu.VIN)), 3600)
	batch.Add("RPUSH", redis.QueueKey(common.HMI.Key(cu.VIN)), msg)
	batch.Add("EXPIRE", redis.QueueKey(common.HMI.Key(cu.VIN)), 3600)

	_, err = client.ExecuteBatch(batch)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable) {
		return
	}

	utils.RespJSON(w, http.StatusOK, common.JSONMessage{
		Message: "OK",
	})
}

func isRedeployAvailable(status string) bool {
	switch status {
	case carupdatestatus.ManifestSucceeded, carupdatestatus.ManifestCanceled,
		carupdatestatus.ManifestError, carupdatestatus.ManifestCancelPending,
		carupdatestatus.RollbackSucceeded, carupdatestatus.ManifestRejected,
		carupdatestatus.RollbackFailed, carupdatestatus.CleanupSucceeded:
		return true
	default:
		return false
	}
}
