package controllers

import (
	"encoding/json"

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

// Operations about workspace
type WorkspaceController struct {
	beego.Controller
}

var paths = map[tools.DataType]map[tools.METHOD]string{
	tools.PEER: { // paths to call to status of peers
		tools.POST: "/status/",
	},
	tools.WORKSPACE: { // paths to call to delete/update workspace on peer destination
		tools.PUT:    "/:id?is_remote=true",
		tools.DELETE: "/:id?is_remote=true",
	},
}

// @Title Search
// @Description search workspace
// @Param	search		path 	string	true		"the word search you want to get"
// @Success 200 {workspace} models.workspace
// @router /search/:search [get]
func (o *WorkspaceController) Search() {
	// store and return Id or post with UUID
	search := o.Ctx.Input.Param(":search")
	o.Data["json"] = oclib.Search(nil, search, oclib.LibDataEnum(oclib.WORKSPACE))
	o.ServeJSON()
}

// @Title Update
// @Description create workspaces
// @Param	id		path 	string	true		"the workspace id you want to get"
// @Param	body		body 	models.workspace	true		"The workspace content"
// @Success 200 {workspace} models.workspace
// @router /:id [put]
func (o *WorkspaceController) Put() {
	// store and return Id or post with UUID
	caller := tools.NewHTTPCaller(paths) // generate a http caller to send to peer shared workspace
	caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
	var res map[string]interface{}
	id := o.Ctx.Input.Param(":id")
	json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
	o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(oclib.WORKSPACE), res, id, caller)
	o.ServeJSON()
}

// @Title Create
// @Description create workspace
// @Param	data		body 	json	true		"body for data content (Json format)"
// @Success 200 {workspace} models.workspace
// @router / [post]
func (o *WorkspaceController) Post() {
	caller := tools.NewHTTPCaller(paths) // generate a http caller to send to peer shared workspace
	caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
	var res map[string]interface{}
	json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
	o.Data["json"] = oclib.StoreOne(oclib.LibDataEnum(oclib.WORKSPACE), res, caller)
	o.ServeJSON()
}

// @Title GetAll
// @Description find workspace by id
// @Success 200 {workspace} models.workspace
// @router / [get]
func (o *WorkspaceController) GetAll() {
	o.Data["json"] = oclib.LoadAll(oclib.LibDataEnum(oclib.WORKSPACE))
	o.ServeJSON()
}

// @Title Get
// @Description find workflow by id
// @Param	id		path 	string	true		"the id you want to get"
// @Success 200 {workspace} models.workspace
// @router /:id [get]
func (o *WorkspaceController) Get() {
	id := o.Ctx.Input.Param(":id")
	o.Data["json"] = oclib.LoadOne(oclib.LibDataEnum(oclib.WORKSPACE), id)
	o.ServeJSON()
}

// @Title Delete
// @Description delete the workspace
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {workspace} delete success!
// @router /:id [delete]
func (o *WorkspaceController) Delete() {
	id := o.Ctx.Input.Param(":id")
	caller := tools.NewHTTPCaller(paths) // generate a http caller to send to peer shared workspace
	caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
	o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(oclib.WORKSPACE), id, caller)
	o.ServeJSON()
}
