package service

import (
	"context"

	"github.com/go-logr/logr"
	"github.com/tidbcloud/devops/pkg/infra/log"
)

type BaseServiceOption func(*BaseService)

func WithDummy(dummy bool) BaseServiceOption {
	return func(s *BaseService) {
		s.dummy = dummy
	}
}

func WithDisableRun(disable bool) BaseServiceOption {
	return func(s *BaseService) {
		s.disableRun = disable
	}
}

type BaseService struct {
	name       string
	dummy      bool
	disableRun bool
}

func NewBaseService(name string, opts ...BaseServiceOption) *BaseService {
	s := &BaseService{
		name: name,
	}

	for _, opt := range opts {
		opt(s)
	}

	return s
}

func (s *BaseService) Name() string {
	return s.name
}

func (s *BaseService) Dummy() bool {
	return s.dummy
}

func (s *BaseService) DisableRun() bool {
	return s.disableRun
}

func (s *BaseService) Logger(ctx context.Context) logr.Logger {
	return log.FromContext(ctx).WithName(s.Name())
}

// WrapContext wrap the context to add some values provided by service.
func (s *BaseService) WrapContext(ctx context.Context) context.Context {
	return log.NewContext(ctx, s.Logger(ctx))
}
