package mocks

import (
	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/db/queries"

	"github.com/go-pg/pg/v10/orm"
	"github.com/google/uuid"
)

type MockSubscriptionPackages struct {
	ListResult   []common.SubscriptionPackage
	LoadResult   *common.SubscriptionPackage
	InsertResult bool
	DBMockHelper
}

func (m *MockSubscriptionPackages) SetListResp(list interface{}) {
	if list != nil {
		result, ok := list.([]common.SubscriptionPackage)
		if ok {
			m.ListResult = result
			return
		}
	}
	m.ListResult = nil
}

func (m *MockSubscriptionPackages) SetLoadResp(item interface{}) {
	if item != nil {
		result, ok := item.(common.SubscriptionPackage)
		if ok {
			m.LoadResult = &result
			return
		}
	}
	m.LoadResult = nil
}

func (m *MockSubscriptionPackages) Delete(model *common.SubscriptionPackage) (orm.Result, error) {
	return m.ORMResponse, m.Error
}

func (m *MockSubscriptionPackages) Insert(model *common.SubscriptionPackage) (orm.Result, error) {
	if m.Error != nil {
		return nil, m.Error
	}

	model.ID = uuid.MustParse("0557bd1d-76d3-41e5-a44e-13c479e55ab0")

	return m.ORMResponse, nil
}

func (m *MockSubscriptionPackages) Update(model *common.SubscriptionPackage) (orm.Result, error) {
	return m.ORMResponse, m.Error
}

func (m *MockSubscriptionPackages) Count(filter *common.SubscriptionPackage) (int, error) {
	return len(m.ListResult), m.Error
}

func (m *MockSubscriptionPackages) Select(filter *common.SubscriptionPackage, paging *queries.PageQueryOptions) ([]common.SubscriptionPackage, error) {
	m.LastFilter = filter
	m.LastPaging = paging

	return m.ListResult, m.Error
}

func (m *MockSubscriptionPackages) Load(model *common.SubscriptionPackage) error {
	filter := *model
	m.LastFilter = &filter

	if m.LoadResult != nil {
		model.ID = m.LoadResult.ID
		model.Name = m.LoadResult.Name
		model.Features = m.LoadResult.Features
	}

	return m.Error
}

func (m *MockSubscriptionPackages) AddFeature(pack *common.SubscriptionPackage, feature *common.SubscriptionFeature) (bool, error) {
	if m.Error != nil {
		return false, m.Error
	}

	pack.AddFeature(feature)

	return m.InsertResult, nil
}

func (m *MockSubscriptionPackages) AssociateFeature(packageid uuid.UUID, featureid uuid.UUID) (bool, error) {
	if m.Error != nil {
		return false, m.Error
	}

	return m.InsertResult, nil
}
