package common

import (
	"fmt"

	"github.com/fiskerinc/cloud-services/pkg/common/dbbasemodel"
	"github.com/google/uuid"
)

type SubscriptionPackage struct {
	ID       uuid.UUID             `json:"id" pg:"type:uuid,default:uuid_generate_v4()"`
	Name     string                `json:"name" validate:"required,max=256"`
	Features []SubscriptionFeature `json:"features,omitempty" pg:"many2many:subscription_package_to_features"`
	dbbasemodel.DBModelBase
}

func (sp *SubscriptionPackage) HasFeature(featureID uuid.UUID) bool {
	for _, feature := range sp.Features {
		if feature.ID == featureID {
			return true
		}
	}

	return false
}

func (sp *SubscriptionPackage) AddFeature(feature *SubscriptionFeature) {
	if !sp.HasFeature(feature.ID) {
		sp.Features = append(sp.Features, *feature)
	}
}

func (sp *SubscriptionPackage) RemoveFeature(feature *SubscriptionFeature) {
	if sp.HasFeature(feature.ID) {
		features := make([]SubscriptionFeature, len(sp.Features)-1)
		for _, item := range sp.Features {
			if feature.ID != item.ID {
				features[len(features)] = item
			}
		}
	}
}

func (sp *SubscriptionPackage) String() string {
	return fmt.Sprintf("SubscriptionPackage<%v>", sp.ID)
}
