package controllers

import (
	"oc-auth/infrastructure"

	beego "github.com/beego/beego/v2/server/web"
)

// Operations about auth
type GroupController struct {
	beego.Controller
}

// @Title Create
// @Description create group
// @Param	id		path 	string	true		"the id you want to get"
// @Success 200 {auth} create success!
// @router /:id [post]
func (o *GroupController) Post() {
	// store and return Id or post with UUID
	id := o.Ctx.Input.Param(":id")
	clientID := ExtractClient(*o.Ctx.Request)
	group, code, err := infrastructure.GetPermissionConnector(clientID).CreateGroup(id)
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"data":  nil,
			"error": err.Error(),
			"code":  code,
		}

	} else {
		o.Data["json"] = map[string]interface{}{
			"data":  group,
			"error": nil,
			"code":  200,
		}
	}
	o.ServeJSON()
}

// @Title GetByUser
// @Description find group by user id
// @Param	id		path 	string	true		"the id you want to get"
// @Success 200 {auth} string
// @router /user/:id [get]
func (o *GroupController) GetByUser() {
	id := o.Ctx.Input.Param(":id")
	clientID := ExtractClient(*o.Ctx.Request)
	group, err := infrastructure.GetPermissionConnector(clientID).GetGroupByUser(id)
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"data":  nil,
			"error": err.Error(),
			"code":  200,
		}

	} else {
		o.Data["json"] = map[string]interface{}{
			"data":  group,
			"error": nil,
			"code":  200,
		}
	}
	o.ServeJSON()
}

// @Title GetAll
// @Description find groups
// @Success 200 {group} string
// @router / [get]
func (o *GroupController) GetAll() {
	clientID := ExtractClient(*o.Ctx.Request)
	group, err := infrastructure.GetPermissionConnector(clientID).GetGroup("")
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"data":  nil,
			"error": err.Error(),
			"code":  200,
		}

	} else {
		o.Data["json"] = map[string]interface{}{
			"data":  group,
			"error": nil,
			"code":  200,
		}
	}
	o.ServeJSON()
}

// @Title Get
// @Description find group by id
// @Param	id		path 	string	true		"the id you want to get"
// @Success 200 {group} string
// @router /:id [get]
func (o *GroupController) Get() {
	id := o.Ctx.Input.Param(":id")
	clientID := ExtractClient(*o.Ctx.Request)
	group, err := infrastructure.GetPermissionConnector(clientID).GetGroup(id)
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"data":  nil,
			"error": err.Error(),
			"code":  200,
		}

	} else {
		o.Data["json"] = map[string]interface{}{
			"data":  group,
			"error": nil,
			"code":  200,
		}
	}
	o.ServeJSON()
}

// @Title Delete
// @Description delete the group
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @router /:id [delete]
func (o *GroupController) Delete() {
	id := o.Ctx.Input.Param(":id")
	clientID := ExtractClient(*o.Ctx.Request)
	group, code, err := infrastructure.GetPermissionConnector(clientID).DeleteGroup(id)
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"data":  nil,
			"error": err.Error(),
			"code":  code,
		}

	} else {
		o.Data["json"] = map[string]interface{}{
			"data":  group,
			"error": nil,
			"code":  200,
		}
	}
	o.ServeJSON()
}

// @Title Clear
// @Description clear the group
// @Success 200 {string} delete success!
// @router /clear [delete]
func (o *GroupController) Clear() {
	clientID := ExtractClient(*o.Ctx.Request)
	group, code, err := infrastructure.GetPermissionConnector(clientID).DeleteGroup("")
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"data":  nil,
			"error": err.Error(),
			"code":  code,
		}

	} else {
		o.Data["json"] = map[string]interface{}{
			"data":  group,
			"error": nil,
			"code":  200,
		}
	}
	o.ServeJSON()
}

// @Title Bind
// @Description bind the group to user
// @Param	user_id		path 	string	true		"The user_id you want to bind"
// @Param	group_id		path 	string	true		"The group_id you want to bind"
// @Success 200 {string} bind success!
// @router /:user_id/:group_id [post]
func (o *GroupController) Bind() {
	user_id := o.Ctx.Input.Param(":user_id")
	group_id := o.Ctx.Input.Param(":group_id")
	clientID := ExtractClient(*o.Ctx.Request)
	group, code, err := infrastructure.GetPermissionConnector(clientID).BindGroup(user_id, group_id)
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"data":  nil,
			"error": err.Error(),
			"code":  code,
		}

	} else {
		o.Data["json"] = map[string]interface{}{
			"data":  group,
			"error": nil,
			"code":  200,
		}
	}
	o.ServeJSON()
}

// @Title UnBind
// @Description unbind the group to user
// @Param	user_id		path 	string	true		"The group_id you want to unbind"
// @Param	group_id		path 	string	true		"The user_id you want to unbind"
// @Success 200 {string} bind success!
// @router /:user_id/:group_id [delete]
func (o *GroupController) UnBind() {
	user_id := o.Ctx.Input.Param(":user_id")
	group_id := o.Ctx.Input.Param(":group_id")
	clientID := ExtractClient(*o.Ctx.Request)
	group, code, err := infrastructure.GetPermissionConnector(clientID).UnBindGroup(user_id, group_id)
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"data":  nil,
			"error": err.Error(),
			"code":  code,
		}

	} else {
		o.Data["json"] = map[string]interface{}{
			"data":  group,
			"error": nil,
			"code":  200,
		}
	}
	o.ServeJSON()
}
