package resources

import (
	"errors"
	"time"

	"cloud.o-forge.io/core/oc-lib/models/common/enum"
	"cloud.o-forge.io/core/oc-lib/models/common/models"
	"cloud.o-forge.io/core/oc-lib/models/common/pricing"
	"cloud.o-forge.io/core/oc-lib/models/utils"
	"cloud.o-forge.io/core/oc-lib/tools"
)

/*
* StorageResource is a struct that represents a storage resource
* it defines the resource storage
 */
type StorageResource struct {
	AbstractIntanciatedResource[*StorageResourceInstance]                  // AbstractResource contains the basic fields of an object (id, name)
	StorageType                                           enum.StorageType `bson:"storage_type" json:"storage_type" default:"-1"` // Type is the type of the storage
	Acronym                                               string           `bson:"acronym,omitempty" json:"acronym,omitempty"`    // Acronym is the acronym of the storage
}

func (d *StorageResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
	return NewAccessor[*StorageResource](tools.STORAGE_RESOURCE, request, func() utils.DBObject { return &StorageResource{} }) // Create a new instance of the accessor
}

func (r *StorageResource) GetType() string {
	return tools.STORAGE_RESOURCE.String()
}

func (abs *StorageResource) ConvertToPricedResource(
	t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
	if t != tools.STORAGE_RESOURCE {
		return nil
	}
	p := abs.AbstractIntanciatedResource.ConvertToPricedResource(t, request)
	priced := p.(*PricedResource)
	return &PricedStorageResource{
		PricedResource: *priced,
	}
}

type StorageResourceInstance struct {
	ResourceInstance[*StorageResourcePartnership]
	Source        string           `bson:"source,omitempty" json:"source,omitempty"` // Source is the source of the storage
	Local         bool             `bson:"local" json:"local"`
	SecurityLevel string           `bson:"security_level,omitempty" json:"security_level,omitempty"`
	SizeType      enum.StorageSize `bson:"size_type" json:"size_type" default:"0"`           // SizeType is the type of the storage size
	SizeGB        int64            `bson:"size,omitempty" json:"size,omitempty"`             // Size is the size of the storage
	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 (ri *StorageResourceInstance) StoreDraftDefault() {
	found := false
	for _, p := range ri.ResourceInstance.Env {
		if p.Attr == "source" {
			found = true
			break
		}
	}
	if !found {
		ri.ResourceInstance.Env = append(ri.ResourceInstance.Env, models.Param{
			Attr:     "source",
			Value:    ri.Source,
			Readonly: true,
		})
	}
	ri.ResourceInstance.StoreDraftDefault()
}

type StorageResourcePartnership struct {
	ResourcePartnerShip[*StorageResourcePricingProfile]
	MaxSizeGBAllowed     float64 `json:"allowed_gb,omitempty" bson:"allowed_gb,omitempty"`
	OnlyEncryptedAllowed bool    `json:"personal_data_allowed,omitempty" bson:"personal_data_allowed,omitempty"`
}

type PrivilegeStoragePricingStrategy int

const (
	BASIC_STORAGE PrivilegeStoragePricingStrategy = iota
	GARANTED_ON_DELAY_STORAGE
	GARANTED_STORAGE
)

func PrivilegeStoragePricingStrategyList() []PrivilegeStoragePricingStrategy {
	return []PrivilegeStoragePricingStrategy{BASIC_STORAGE, GARANTED_ON_DELAY_STORAGE, GARANTED_STORAGE}
}

func (t PrivilegeStoragePricingStrategy) String() string {
	return [...]string{"NO MEMORY HOLDING", "KEEPED ON MEMORY GARANTED DURING DELAY", "KEEPED ON MEMORY GARANTED"}[t]
}

type StorageResourcePricingStrategy int

const (
	PER_DATA_STORED StorageResourcePricingStrategy = iota
	PER_TB_STORED
	PER_GB_STORED
	PER_MB_STORED
	PER_KB_STORED
)

func StorageResourcePricingStrategyList() []StorageResourcePricingStrategy {
	return []StorageResourcePricingStrategy{PER_DATA_STORED, PER_TB_STORED, PER_GB_STORED, PER_MB_STORED, PER_KB_STORED}
}

func (t StorageResourcePricingStrategy) String() string {
	return [...]string{"PER DATA STORED", "PER TB STORED", "PER GB STORED", "PER MB STORED", "PER KB STORED"}[t]
}

func (t StorageResourcePricingStrategy) GetStrategy() string {
	return [...]string{"PER_DATA_STORED", "PER_GB_STORED", "PER_MB_STORED", "PER_KB_STORED"}[t]
}

func (t StorageResourcePricingStrategy) GetStrategyValue() int {
	return int(t)
}

func ToStorageResourcePricingStrategy(i int) StorageResourcePricingStrategy {
	return StorageResourcePricingStrategy(i)
}

func (t StorageResourcePricingStrategy) GetQuantity(amountOfDataGB float64) (float64, error) {
	switch t {
	case PER_DATA_STORED:
		return amountOfDataGB, nil
	case PER_TB_STORED:
		return amountOfDataGB * 1000, nil
	case PER_GB_STORED:
		return amountOfDataGB, nil
	case PER_MB_STORED:
		return (amountOfDataGB * 1000), nil
	case PER_KB_STORED:
		return amountOfDataGB * 1000000, nil
	}
	return 0, errors.New("pricing strategy not found")
}

type StorageResourcePricingProfile struct {
	pricing.ExploitPricingProfile[StorageResourcePricingStrategy] // ExploitPricingProfile is the pricing profile of a storage it means that we exploit the resource for an amount of continuous time
}

func (p *StorageResourcePricingProfile) IsPurchased() bool {
	return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE
}

func (p *StorageResourcePricingProfile) GetPrice(amountOfData float64, val float64, start time.Time, end time.Time, params ...string) (float64, error) {
	return p.Pricing.GetPrice(amountOfData, val, start, &end)
}

type PricedStorageResource struct {
	PricedResource
	UsageStorageGB float64 `json:"storage_gb,omitempty" bson:"storage_gb,omitempty"`
}

func (r *PricedStorageResource) GetType() tools.DataType {
	return tools.STORAGE_RESOURCE
}

func (r *PricedStorageResource) GetPrice() (float64, error) {
	if r.UsageStart == nil || r.UsageEnd == nil {
		return 0, errors.New("usage start and end must be set")
	}
	if r.SelectedPricing == nil {
		return 0, errors.New("selected pricing must be set")
	}
	pricing := *r.SelectedPricing
	var err error
	amountOfData := float64(1)
	if pricing.GetOverrideStrategyValue() >= 0 {
		amountOfData, err = ToStorageResourcePricingStrategy(pricing.GetOverrideStrategyValue()).GetQuantity(r.UsageStorageGB)
		if err != nil {
			return 0, err
		}
	}
	return pricing.GetPrice(amountOfData, r.ExplicitBookingDurationS, *r.UsageStart, *r.UsageEnd)
}
