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"
// @Param	is_draft		query 	string	false		"draft wished"
// @Success 200 {workspace} models.workspace
// @router /search/:search [get]
func (o *WorkspaceController) 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.WORKSPACE), user, peerID, groups, nil).Search(nil, search, isDraft == "true")
	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() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	// 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.NewRequest(oclib.LibDataEnum(oclib.WORKSPACE), user, peerID, groups, caller).UpdateOne(res, id)
	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() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	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.NewRequest(oclib.LibDataEnum(oclib.WORKSPACE), user, peerID, groups, caller).StoreOne(res)
	o.ServeJSON()
}

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

// @Title Get
// @Description find workflow by idisDraft := o.Ctx.Input.Query("is_draft")
// @Param	id		path 	string	true		"the id you want to get"
// @Success 200 {workspace} models.workspace
// @router /:id [get]
func (o *WorkspaceController) Get() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	id := o.Ctx.Input.Param(":id")
	o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.WORKSPACE), user, peerID, groups, nil).LoadOne(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() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	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.NewRequest(oclib.LibDataEnum(oclib.WORKSPACE), user, peerID, groups, caller).DeleteOne(id)
	o.ServeJSON()
}