package toolkit

import (
	"fmt"
	"os"
	"testing"

	"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
	"github.com/stretchr/testify/mock"
)

// MarkIntegrationTest marks a test as being an integration test. It will be
// skipped if the ISLE_INTEGRATION_TEST envvar isn't set.
func MarkIntegrationTest(t *testing.T) {
	if os.Getenv("ISLE_INTEGRATION_TEST") == "" {
		t.Skip("Skipped because ISLE_INTEGRATION_TEST isn't set")
	}
}

// NewTestLogger returns a Logger which should be used for testing purposes. The
// log level of the Logger can be adjusted using the ISLE_LOG_LEVEL envvar.
func NewTestLogger(t *testing.T) *mlog.Logger {
	level := mlog.LevelInfo
	if levelStr := os.Getenv("ISLE_LOG_LEVEL"); levelStr != "" {
		if level = LogLevelFromString(levelStr); level == nil {
			panic(fmt.Sprintf("invalid log level: %q", levelStr))
		}
	}

	return mlog.NewLogger(&mlog.LoggerOpts{
		MessageHandler: mlog.NewTestMessageHandler(t),
		MaxLevel:       level.Int(),
	})
}

// MockArg returns a value which can be used as a [mock.Call] argument, and
// which will match any value of type T. If T is an interface then also values
// implementing that interface will be matched.
func MockArg[T any]() any {
	return mock.MatchedBy(func(T) bool { return true })
}