package storage

import (
	"encoding/json"

	"cloud.o-forge.io/core/oc-lib/models/resource_model"
	"cloud.o-forge.io/core/oc-lib/models/utils"
	"cloud.o-forge.io/core/oc-lib/tools"
)

type StorageType int

// StorageType - Enum that defines the type of storage
const (
	GB StorageType = iota
	MB
	KB
)

var argoType = [...]string{
	"Gi",
	"Mi",
	"Ki",
}

// New creates a new instance of the StorageResource struct
func (dma StorageType) ToArgo() string {
	return argoType[dma]
}

type URL struct {
	Protocol string `bson:"protocol,omitempty" json:"protocol,omitempty"` // Protocol is the protocol of the URL
	Path     string `bson:"path,omitempty" json:"path,omitempty"`         // Path is the path of the URL
}

/*
* StorageResource is a struct that represents a storage resource
* it defines the resource storage
 */
type StorageResource struct {
	resource_model.AbstractResource             // AbstractResource contains the basic fields of an object (id, name)
	Acronym                         string      `bson:"acronym,omitempty" json:"acronym,omitempty"`       // Acronym is the acronym of the storage
	Type                            string      `bson:"type,omitempty" json:"type,omitempty"`             // Type is the type of the storage
	SizeType                        StorageType `bson:"size_type" json:"size_type" default:"0"`           // SizeType is the type of the storage size
	Size                            uint        `bson:"size,omitempty" json:"size,omitempty"`             // Size is the size of the storage
	Url                             *URL        `bson:"url,omitempty" json:"url,omitempty"`               // Will allow to select between several protocols
	Local                           bool        `bson:"local" json:"local"`                               // Local is a flag that indicates if the storage is local
	Encryption                      bool        `bson:"encryption,omitempty" json:"encryption,omitempty"` // Encryption is a flag that indicates if the storage is encrypted
	Redundancy                      string      `bson:"redundancy,omitempty" json:"redundancy,omitempty"` // Redundancy is the redundancy of the storage
	Throughput                      string      `bson:"throughput,omitempty" json:"throughput,omitempty"` // Throughput is the throughput of the storage
}

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

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