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/db/queries"
	"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"
)

// HandleCarUpdateCancel godoc
// @Summary Cancel car update
// @Description Cancels 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 cancel"
// @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}/cancel [post]
func HandleCarUpdateCancel(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_cancel.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 HandleCarUpdateCancel")
		}
	}()

	// If this update was from aftersales, then we just mark as canceled
	if cu.UpdateSource == common.UPDATE_SOURCE_AFTERSALES {
		err = cancelUpdateAftersales(req, cu, carupdates)
	} else {
		err = cancelUpdateOTA(req, cu, carupdates)
	}

	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable) {
		return
	}

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

func cancelUpdateAftersales(req common.CarUpdateRequest, cu *common.CarUpdate, carupdates queries.CarUpdatesInterface) (err error) {
	current := common.CarUpdate{
		ID:     req.CarUpdateID,
		Status: carupdatestatus.ManifestCanceled,
	}
	_, err = carupdates.UpdateStatus(&current)
	if err != nil {
		return
	}

	return
}

func cancelUpdateOTA(req common.CarUpdateRequest, cu *common.CarUpdate, carupdates queries.CarUpdatesInterface) (err error) {
	current := common.CarUpdate{
		ID:     req.CarUpdateID,
		Status: carupdatestatus.ManifestCancelPending,
	}
	_, err = carupdates.UpdateStatus(&current)
	if err != nil {
		return
	}

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

	key := redis.CarUpdateStatusHashKey(req.CarUpdateID)
	msg, err := json.Marshal(common.Message{
		Handler: handlers.UpdateManifestCancel,
		Data:    req,
	})

	batch := redis.NewRedisBatchCommands()
	batch.Add(re.Args{}.Add("HSET").Add(key).AddFlat(common.CarUpdateProgress{
		CarUpdateID: req.CarUpdateID,
		Status:      carupdatestatus.ManifestCancelPending,
	})...)
	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 err != nil {
		return
	}

	return
}
