package controllers

import (
	"encoding/json"

	"cloud.o-forge.io/core/oc-lib/tools"
	beego "github.com/beego/beego/v2/server/web"
)

type State int

const (
	ALIVE State = iota
	REDUCED_SERVICE
	DB_FALLOUT
	DEAD
)

func (s State) String() string {
	return [...]string{"alive", "reduced service", "database fallout", "dead"}[s]
}

// Operations about workflow
type StatusController struct {
	beego.Controller
}

// @Title Status
// @Description get peer status if it's alive
// @Param	body		body 	list of addresses	true		"The addresses list"
// @Success 200 {status} models.status
// @router / [post]
func (o *StatusController) Status() {
	var address map[string]string
	json.Unmarshal(o.Ctx.Input.CopyBody(10000), &address)
	api := tools.API{}
	state, services, err := api.CheckRemoteAPIs([]tools.DataType{tools.BOOKING, tools.WORKFLOW, tools.WORKSPACE,
		tools.COLLABORATIVE_AREA, tools.DATA_RESOURCE}) // check if the services are alive
	errSTR := ""
	if err != nil {
		errSTR = err.Error()
	}
	o.Data["json"] = map[string]interface{}{ // return the status
		"data": map[string]interface{}{
			"state":    state.String(),
			"code":     state.EnumIndex(),
			"services": services,
		},
		"code":  200,
		"error": errSTR,
	}
	o.ServeJSON()
}
