package mocks

import (
	"time"

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

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

type MockSubscriptions struct {
	ListResult []common.Subscription
	ItemResult *common.Subscription
	DBMockHelper
}

// Select returns list of drivers
func (s *MockSubscriptions) Select(filter *common.Subscription) ([]common.Subscription, error) {
	return s.ListResult, s.Error
}

func (s *MockSubscriptions) Insert(subtype *common.Subscription) (orm.Result, error) {
	return s.ORMResponse, s.Error
}

func (s *MockSubscriptions) Update(subtype *common.Subscription) (orm.Result, error) {
	return s.ORMResponse, s.Error
}

func (s *MockSubscriptions) Delete(req *queries.SubscriptionDeleteRequest) (orm.Result, error) {
	return s.ORMResponse, s.Error
}

func (s *MockSubscriptions) Load(sub *common.Subscription) error {
	return s.Error

}

func (s *MockSubscriptions) Count(filter *common.Subscription) (int, error) {
	return len(s.ListResult), s.Error
}

func (s *MockSubscriptions) Create(subtype *common.SubscriptionType, carToDriver *common.CarToDriver) (*common.Subscription, error) {
	if s.ItemResult == nil {
		return nil, s.Error
	}

	now := time.Now()
	s.ItemResult.Name = subtype.Name
	s.ItemResult.SubscriptionTypeID = subtype.ID
	s.ItemResult.CreatedAt = &now
	s.ItemResult.UpdatedAt = &now

	return s.ItemResult, s.Error
}
