package handlers

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

	"github.com/fiskerinc/cloud-services/pkg/superset"
	"github.com/fiskerinc/cloud-services/pkg/utils"
	"github.com/fiskerinc/cloud-services/pkg/loggerdataresp"
)

// HandleSupersetEmbeddedDashboardsGet godoc
// @Summary Get the list of embeddable superset dashboards
// @Description Returns list of dashboard embedding id and their title
// @Accept  json
// @Produce  json
// @Param Authorization header string false "Bearer <ID token>"
// @Param Api-Key header string false "<API token>"
// @Success 200 {object} []superset.EmbeddableDashboard
// @Failure 401 {object} common.JSONError "Unauthorized"
// @Failure 503 {object} common.JSONError "Service unavailable"
// @Router /dashboard/embedded-dashboards [get]
func HandleSupersetEmbeddedDashboardsGet(w http.ResponseWriter, r *http.Request) {
	redisClient := services.RedisClientPool().GetFromPool()
	defer redisClient.Close()

	accToken, err := getAccessTokenFunc(redisClient)
	if loggerdataresp.BadDataErrorResp(w, err, http.StatusServiceUnavailable) {
		return
	}
	dashboards, err := superset.GetEmbeddableDashboards(accToken)

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

	utils.RespJSON(w, http.StatusOK, &dashboards)
}
