//go:build end2end
// +build end2end

package end2end

import (
	"testing"

	"git.gocasts.ir/ebhomengo/niki/adapter/kavenegar"
	"git.gocasts.ir/ebhomengo/niki/config"
	"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
	"git.gocasts.ir/ebhomengo/niki/service"
	authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
	benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
)

var (
	services       *service.Service
	testServer     *setup.TestServer
	testContainer  *setup.TestContainer
	testSmsAdapter *setup.TestSmsAdapter
)

func TestMain(m *testing.M) {
	// Start test containers
	testContainer = setup.NewTestContainer()
	testContainer.Start()
	defer testContainer.Stop()

	// Run MariaDB Migrations
	teardown := setup.MigrateMariaDB(testContainer.GetMariaDBConfig())
	defer teardown()

	// Setup Test SMS Adapter
	testSmsAdapter = setup.NewTestSmsAdapter()

	// Setup Services and Test Server
	cfg := getConfig()
	services = service.New(
		cfg,
		testContainer.GetMariaDBConnection(),
		testContainer.GetRedisDBConnection(),
		testSmsAdapter,
	)
	testServer = setup.NewTestServer(cfg, services)

	// Run the tests
	m.Run()
}

func getConfig() config.Config {
	return config.Config{
		HTTPServer: config.HTTPServer{Port: 42069},
		Mysql:      testContainer.GetMariaDBConfig(),
		BenefactorAuth: authservice.Config{
			SignKey:               "jwt_secret_test_nik",
			AccessExpirationTime:  config.Default().BenefactorAuth.AccessExpirationTime,
			RefreshExpirationTime: config.Default().BenefactorAuth.RefreshExpirationTime,
			AccessSubject:         config.Default().BenefactorAuth.AccessSubject,
			RefreshSubject:        config.Default().BenefactorAuth.RefreshSubject,
		},
		AdminAuth: authservice.Config{
			SignKey:               "admin-jwt_secret_test_nik",
			AccessExpirationTime:  config.Default().BenefactorAuth.AccessExpirationTime,
			RefreshExpirationTime: config.Default().BenefactorAuth.RefreshExpirationTime,
			AccessSubject:         config.Default().BenefactorAuth.AccessSubject,
			RefreshSubject:        config.Default().BenefactorAuth.RefreshSubject,
		},
		Redis: testContainer.GetRedisConfig(),
		KavenegarSmsProvider: kavenegar.Config{
			APIKey: "",
			Sender: "",
		},
		BenefactorSvc: benefactorservice.Config{
			LengthOfOtpCode: 5,
			OtpChars:        config.OtpChars,
			OtpExpireTime:   config.OtpExpireTime,
		},
	}
}
