package controllers

import (
	"encoding/json"
	"errors"
	"fmt"

	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 workflow
type WorkflowController struct {
	beego.Controller
}

var paths = map[tools.DataType]map[tools.METHOD]string{ // paths to call other OC services
	tools.BOOKING: {
		tools.POST:   "/booking/",
		tools.DELETE: "/booking/:id",
	},
	tools.PEER: {
		tools.POST: "/status/",
	},
	tools.WORKFLOW: {
		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 *WorkflowController) 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.WORKFLOW), user, peerID, groups, nil).Search(nil, search, isDraft == "true")
	o.ServeJSON()
}

// @Title Update
// @Description create workflows
// @Param	id		path 	string	true		"the workflowid you want to get"
// @Param	body		body 	models.workflow	true		"The workflow content"
// @Success 200 {object} models.workflow
// @router /:id [put]
func (o *WorkflowController) 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(10000000), &res)
	fmt.Println("res", res["links"])
	caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
	caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
	o.Ctx.Input.Param("is_remote")
	data := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW), user, peerID, groups, caller).UpdateOne(res, id)
	o.Data["json"] = data
	o.ServeJSON()
}

// @Title Create
// @Description create workflows
// @Param	data		body 	json	true		"body for data content (Json format)"
// @Success 200 {object} models.workflow
// @router / [post]
func (o *WorkflowController) Post() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	var res map[string]interface{}
	json.Unmarshal(o.Ctx.Input.CopyBody(10000000), &res)
	caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
	caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
	data := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW), user, peerID, groups, caller).StoreOne(res)
	o.Data["json"] = data
	o.ServeJSON()
}

// @Title Publish
// @Description create workflows
// @Param	id		path 	string	true		"the workflowid you want to get"
// @Success 200 {object} models.workflow
// @router /publish/:id [post]
func (o *WorkflowController) Publish() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	id := o.Ctx.Input.Param(":id")
	data := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW), user, peerID, groups, nil).LoadOne(id)
	if data.Data != nil { // copy existing workflow as a resource
		o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW), user, peerID, groups, nil).CopyOne(data.Data.Serialize(data.Data))
	} else {
		o.Data["json"] = data
	}
	o.ServeJSON()
}

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

// @Title Get
// @Description find workflow by workflowid
// @Param	id		path 	string	true		"the workflowid you want to get"
// @Success 200 {workflow} models.workflow
// @router /:id [get]
func (o *WorkflowController) Get() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	id := o.Ctx.Input.Param(":id")
	caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
	caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
	o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW), user, peerID, groups, caller).LoadOne(id)
	o.ServeJSON()
}

// @Title Delete
// @Description delete the workflow
// @Param	id		path 	string	true		"The workflowId you want to delete"
// @Success 200 {string} delete success!
// @router /:id [delete]
func (o *WorkflowController) Delete() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	id := o.Ctx.Input.Param(":id")
	caller := tools.NewHTTPCaller(paths) // create a new HTTP caller
	caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
	o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW), user, peerID, groups, caller).DeleteOne(id)
	o.ServeJSON()
}

// @Title Check
// @Description check booking
// @Param	id				path 	string		"the booking workflow id"
// @Param	start_date		path 	string		"the booking start date format "2006-01-02T15:04:05"
// @Param	end_date		path 	string		"the booking end date" format 2006-01-02T15:04:05"
// @Success 200 {object} models.object
// @router /check/:id/:start_date/:end_date [get]
func (o *WorkflowController) Check() {
	// store and return Id or post with UUID
	/*
	 * Check if the booking is available
	 */
	id := o.Ctx.Input.Param(":id")
	if id == "" {
		o.Data["json"] = map[string]interface{}{
			"data": map[string]interface{}{
				"is_available": false,
			},
			"code":  400,
			"error": errors.New("invalid date format"),
		}
	} else {
		user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
		res := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW), user, peerID, groups, nil).LoadOne(id)
		if res.Data != nil {
			workflow := res.ToWorkflow()
			caller := tools.NewHTTPCaller(map[tools.DataType]map[tools.METHOD]string{ // paths to call other OC services
				tools.PEER: {
					tools.POST: "/status/",
				},
				tools.BOOKING: {
					tools.GET: "/booking/check/:id/" + o.Ctx.Input.Param(":start_date") + "/" + o.Ctx.Input.Param(":end_date"),
				},
			})
			isAvailable, err := workflow.CheckBooking(caller) // check booking
			fmt.Println("isAvailable", isAvailable, o.Ctx.Input.Param(":start_date"), o.Ctx.Input.Param(":end_date"), err)
			code := 200
			if !isAvailable { // if not available then its a conflict
				code = 409
			}
			o.Data["json"] = map[string]interface{}{
				"data": map[string]interface{}{
					"is_available": isAvailable,
				},
				"code":  code,
				"error": err,
			}
		} else {
			o.Data["json"] = res
		}
	}
	o.ServeJSON()
}
