package shallow_collaborative_area

import (
	"encoding/json"

	"cloud.o-forge.io/core/oc-lib/models/utils"
	"cloud.o-forge.io/core/oc-lib/tools"
	"github.com/google/uuid"
)

type ShallowCollaborativeArea struct {
	utils.AbstractObject
	IsSent      bool                   `json:"is_sent" bson:"-"`
	CreatorID   string                 `json:"peer_id,omitempty" bson:"peer_id,omitempty" validate:"required"`
	Version     string                 `json:"version,omitempty" bson:"version,omitempty"`
	Description string                 `json:"description,omitempty" bson:"description,omitempty" validate:"required"`
	Attributes  map[string]interface{} `json:"attributes,omitempty" bson:"attributes,omitempty"`
	Workspaces  []string               `json:"workspaces,omitempty" bson:"workspaces,omitempty"`
	Workflows   []string               `json:"workflows,omitempty" bson:"workflows,omitempty"`
	Peers       []string               `json:"peers,omitempty" bson:"peers,omitempty"`
	Rules       []string               `json:"rules,omitempty" bson:"rules,omitempty"`
}

func (ao *ShallowCollaborativeArea) GetID() string {
	return ao.UUID
}

func (r *ShallowCollaborativeArea) GenerateID() {
	if r.UUID == "" {
		r.UUID = uuid.New().String()
	}
}

func (d *ShallowCollaborativeArea) GetName() string {
	return d.Name
}

func (d *ShallowCollaborativeArea) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
	data := New()
	data.Init(tools.COLLABORATIVE_AREA, caller)
	return data
}

func (dma *ShallowCollaborativeArea) Deserialize(j map[string]interface{}) utils.DBObject {
	b, err := json.Marshal(j)
	if err != nil {
		return nil
	}
	json.Unmarshal(b, dma)
	return dma
}

func (dma *ShallowCollaborativeArea) Serialize() map[string]interface{} {
	var m map[string]interface{}
	b, err := json.Marshal(dma)
	if err != nil {
		return nil
	}
	json.Unmarshal(b, &m)
	return m
}
