package collaborative_area

import (
	"encoding/json"

	"cloud.o-forge.io/core/oc-lib/models/collaborative_area/rules/rule"
	"cloud.o-forge.io/core/oc-lib/models/peer"
	"cloud.o-forge.io/core/oc-lib/models/utils"
	w "cloud.o-forge.io/core/oc-lib/models/workflow"
	"cloud.o-forge.io/core/oc-lib/models/workspace"
	"cloud.o-forge.io/core/oc-lib/tools"
	"github.com/google/uuid"
)

// SharedWorkspace is a struct that represents a shared workspace
// WARNING : it got a shallow object version, this one is the full version
// full version is the one used by API
// other one is a shallow version used by the Lib for import cycle problem purposes
type CollaborativeArea struct {
	utils.AbstractObject                        // AbstractObject contains the basic fields of an object (id, name)
	IsSent               bool                   `json:"is_sent" bson:"-"`                                                       // IsSent is a flag that indicates if the workspace is sent
	CreatorID            string                 `json:"peer_id,omitempty" bson:"peer_id,omitempty" validate:"required"`         // CreatorID is the ID of the creator
	Version              string                 `json:"version,omitempty" bson:"version,omitempty"`                             // Version is the version of the workspace
	Description          string                 `json:"description,omitempty" bson:"description,omitempty" validate:"required"` // Description is the description of the workspace
	Attributes           map[string]interface{} `json:"attributes,omitempty" bson:"attributes,omitempty"`                       // Attributes is the attributes of the workspace (TODO)
	Workspaces           []string               `json:"workspaces,omitempty" bson:"workspaces,omitempty"`                       // Workspaces is the workspaces of the workspace
	Workflows            []string               `json:"workflows,omitempty" bson:"workflows,omitempty"`                         // Workflows is the workflows of the workspace
	Peers                []string               `json:"peers,omitempty" bson:"peers,omitempty"`                                 // Peers is the peers of the workspace
	Rules                []string               `json:"rules,omitempty" bson:"rules,omitempty"`                                 // Rules is the rules of the workspace

	SharedRules      []*rule.Rule           `json:"shared_rules,omitempty" bson:"-"`      // SharedRules is the shared rules of the workspace
	SharedWorkspaces []*workspace.Workspace `json:"shared_workspaces,omitempty" bson:"-"` // SharedWorkspaces is the shared workspaces of the workspace
	SharedWorkflows  []*w.Workflow          `json:"shared_workflows,omitempty" bson:"-"`  // SharedWorkflows is the shared workflows of the workspace
	SharedPeers      []*peer.Peer           `json:"shared_peers,omitempty" bson:"-"`      // SharedPeers is the shared peers of the workspace
}

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

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

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

func (d *CollaborativeArea) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
	data := New()                               // Create a new instance of the accessor
	data.Init(tools.COLLABORATIVE_AREA, caller) // Initialize the accessor with the SHARED_WORKSPACE model type
	return data
}

func (dma *CollaborativeArea) 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 *CollaborativeArea) 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
}
