package controllers

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

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

// @Title GetAll
// @Description find booking by id
// @Param	is_draft		query 	string	false		"draft wished"
// @Success 200 {booking} models.booking
// @router / [get]
func (o *DatacenterController) GetAll() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	isDraft := o.Ctx.Input.Query("is_draft")
	storages := oclib.NewRequest(oclib.LibDataEnum(oclib.STORAGE_RESOURCE), user, peerID, groups, nil).Search(&dbs.Filters{
		Or: map[string][]dbs.Filter{
			"abstractinstanciatedresource.abstractresource.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
		},
	}, "", isDraft == "true")
	computes := oclib.NewRequest(oclib.LibDataEnum(oclib.COMPUTE_RESOURCE), user, peerID, groups, nil).Search(&dbs.Filters{
		Or: map[string][]dbs.Filter{
			"abstractinstanciatedresource.abstractresource.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
		},
	}, "", isDraft == "true")
	storages.Data = append(storages.Data, computes.Data...)
	if storages.Err != "" {
		storages.Err += " - " + computes.Err
	}
	o.Data["json"] = storages
	o.ServeJSON()
}

// @Title Get
// @Description find booking by id
// @Param	id		path 	string	true		"the id you want to get"
// @Param	is_draft		query 	string	false		"draft wished"
// @Success 200 {booking} models.booking
// @router /:id [get]
func (o *DatacenterController) Get() {
	user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
	isDraft := o.Ctx.Input.Query("is_draft")
	id := o.Ctx.Input.Param(":id")
	storages := oclib.NewRequest(oclib.LibDataEnum(oclib.STORAGE_RESOURCE), user, peerID, groups, nil).Search(&dbs.Filters{
		Or: map[string][]dbs.Filter{
			"abstractinstanciatedresource.abstractresource.abstractobject.id":         {{Operator: dbs.EQUAL.String(), Value: id}},
			"abstractinstanciatedresource.abstractresource.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
		},
	}, "", isDraft == "true")
	if len(storages.Data) == 0 {
		computes := oclib.NewRequest(oclib.LibDataEnum(oclib.COMPUTE_RESOURCE), user, peerID, groups, nil).Search(&dbs.Filters{
			Or: map[string][]dbs.Filter{
				"abstractinstanciatedresource.abstractresource.abstractobject.id":         {{Operator: dbs.EQUAL.String(), Value: id}},
				"abstractinstanciatedresource.abstractresource.abstractobject.creator_id": {{Operator: dbs.EQUAL.String(), Value: peerID}},
			},
		}, "", isDraft == "true")
		if len(computes.Data) == 0 {
			o.Data["json"] = map[string]interface{}{
				"data": nil,
				"code": computes.Code,
				"err":  computes.Err,
			}
		} else {
			o.Data["json"] = map[string]interface{}{
				"data": computes.Data[0],
				"code": computes.Code,
				"err":  computes.Err,
			}
		}

	} else {
		o.Data["json"] = map[string]interface{}{
			"data": storages.Data[0],
			"code": storages.Code,
			"err":  storages.Err,
		}
	}
	o.ServeJSON()
}
