package controllers

import (
	"encoding/json"

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

// Operations about rule
type RuleController struct {
	beego.Controller
}

// @Title Search
// @Description search rule
// @Param	search		path 	string	true		"the word search you want to get"
// @Param	is_draft		query 	string	false		"draft wished"
// @Success 200 {rule} models.rule
// @router /search/:search [get]
func (o *RuleController) Search() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	// store and return Id or post with UUID
	search := o.Ctx.Input.Param(":search")
	isDraft := o.Ctx.Input.Query("is_draft")
	o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).Search(nil, search, isDraft == "true")
	o.ServeJSON()
}

// @Title Update
// @Description create rules
// @Param	id		path 	string	true		"the rule id you want to get"
// @Param	body		body 	models.rule	true		"The rule content"
// @Success 200 {rule} models.rule
// @router /:id [put]
func (o *RuleController) Put() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	// store and return ID or post with UUID
	var res map[string]interface{}
	id := o.Ctx.Input.Param(":id")
	json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
	o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).UpdateOne(res, id)
	o.ServeJSON()
}

// @Title Create
// @Description create rule
// @Param	data		body 	json	true		"body for data content (Json format)"
// @Success 200 {rule} models.rule
// @router / [post]
func (o *RuleController) Post() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	var res map[string]interface{}
	json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
	o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).StoreOne(res)
	o.ServeJSON()
}

// @Title GetAll
// @Description find rule by id
// @Param	is_draft		query 	string	false		"draft wished"
// @Success 200 {rule} models.rule
// @router / [get]
func (o *RuleController) GetAll() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	isDraft := o.Ctx.Input.Query("is_draft")
	o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).LoadAll(isDraft == "true")
	o.ServeJSON()
}

// @Title Get
// @Description find rule by id
// @Param	id		path 	string	true		"the id you want to get"
// @Success 200 {rule} models.rule
// @router /:id [get]
func (o *RuleController) Get() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	id := o.Ctx.Input.Param(":id")
	o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).LoadOne(id)
	o.ServeJSON()
}

// @Title Delete
// @Description delete the rule
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {rule} delete success!
// @router /:id [delete]
func (o *RuleController) Delete() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	id := o.Ctx.Input.Param(":id")
	o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).DeleteOne(id)
	o.ServeJSON()
}
