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 MockSubscriptionFeatures struct {
	ListResult []common.SubscriptionFeature
	LoadResult *common.SubscriptionFeature
	DBMockHelper
}

func (m *MockSubscriptionFeatures) SetListResp(list interface{}) {
	if list != nil {
		m.ListResult = list.([]common.SubscriptionFeature)
	} else {
		m.ListResult = nil
	}
}

func (m *MockSubscriptionFeatures) SetLoadResp(item interface{}) {
	if item != nil {
		m.LoadResult = item.(*common.SubscriptionFeature)
	} else {
		m.LoadResult = nil
	}
}

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

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

	model.ID = uuid.MustParse("ecfb89e0-ca03-4aa9-a43a-a9d703256edb")

	return m.ORMResponse, nil
}

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

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

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

	return m.ListResult, m.Error
}

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

	if m.Error != nil {
		return m.Error
	}

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

	return nil
}
