package handlers

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

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/logger"
	"github.com/fiskerinc/cloud-services/pkg/utils"

	ch "github.com/ClickHouse/clickhouse-go/v2"
	"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"
)

// HandleCanSignalListGet godoc
// @Summary Lists of Can Signals used in Feature Table
// @Description Returns a list of can signals Requires API token permission.
// @Accept  json
// @Produce  json
// @Param Authorization header string false "Bearer <ID token>"
// @Param Api-Key header string false "<API token>"
// @Success 200 {object} common.JSONDBQueryResult{data=[]common.CANSignalNameList} "list of cars"
// @Failure 400 {object} common.JSONError "Bad request"
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /can_signals_list [get]
func HandleCanSignalListGet(w http.ResponseWriter, r *http.Request) {

	var canlist []common.CANSignalNameList
	conn, err := services.GetClickhouseConn()

	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable) {
		logger.Error().Err(err).Msg("cannot get clickhouse client")
		return
	}
	chCtx := ch.Context(context.Background())
	err = conn.Select(chCtx, &canlist, "select Signal_Name from ml_var_list_table where Is_Feature=True")
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable) {
		return
	}

	utils.RespJSON(w, http.StatusOK, common.JSONDBQueryResult{Data: canlist})
}
