package rule

import (
	"encoding/json"

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

/*
* Rule is a struct that represents a rule of a shared workspace
 */
type Rule struct {
	utils.AbstractObject          // AbstractObject contains the basic fields of an object (id, name)
	Description          string   `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the rule
	Condition            string   `json:"condition,omitempty" bson:"condition,omitempty"`     // NOT DEFINITIVE TO SPECIFICATION
	Actions              []string `json:"actions,omitempty" bson:"actions,omitempty"`         // NOT DEFINITIVE TO SPECIFICATION
}

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

func (r *Rule) GenerateID() {
	r.UUID = uuid.New().String()
}

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

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

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