package initialize_test

import (
	"testing"
	"time"

	"gitea.pena/PenaSide/common/mongo"
	"gitea.pena/PenaSide/customer/internal/initialize"
	"gitea.pena/PenaSide/customer/internal/models"
	"github.com/golang-jwt/jwt/v5"
	"github.com/stretchr/testify/assert"
)

func TestConfiguration(t *testing.T) {
	t.Run("Успешная инициализация конфигурации", func(t *testing.T) {
		defaultConfiguration := setDefaultTestingENV(t)

		assert.NotPanics(t, func() {
			configuration, err := initialize.Configuration("")

			assert.NoError(t, err)
			assert.Equal(t, defaultConfiguration, configuration)
		})
	})

	t.Run("Ошибка при наличии кривого url", func(t *testing.T) {
		setDefaultTestingENV(t)
		t.Setenv("AUTH_MICROSERVICE_USER_URL", "url")

		assert.NotPanics(t, func() {
			configuration, err := initialize.Configuration("")

			assert.Error(t, err)
			assert.Nil(t, configuration)
		})
	})

	t.Run("Ошибка при отсутствии обязательного env", func(t *testing.T) {
		assert.NotPanics(t, func() {
			configuration, err := initialize.Configuration("")

			assert.Error(t, err)
			assert.Nil(t, configuration)
		})
	})
}

func setDefaultTestingENV(t *testing.T) *models.Config {
	t.Helper()

	defaultAuthURL := "http://www.auth.ru/user"

	defaultHubAdminURL := "http://www.hubadmin.ru/tariff"

	defaultCurrencyURL := "http://www.currency.ru/change"

	defaultConfiguration := models.Config{
		ClientHttpURL:              "localhost:8080",
		AdminHttpURL:               "localhost:8081",
		GrpcURL:                    "localhost:8081",
		GrpcDomain:                 "domain",
		AuthMicroserviceURL:        defaultAuthURL,
		KafkaTopicTariff:           "topic",
		KafkaBrokers:               []string{"localhost:8080", "localhost:8081"},
		HubadminMicroserviceURL:    defaultHubAdminURL,
		CurrencyMicroserviceURL:    defaultCurrencyURL,
		DiscountMicroserviceGRPC:   "domen",
		PaymentMicroserviceGRPC:    "domen",
		VerificationMicroservice:   "domen",
		TemplategenMicroserviceURL: "domen",
		ExternalCfg: models.ExternalCfg{
			JwtCfg: models.JWTConfiguration{
				PrivateKey: "jwt private key",
				PublicKey:  "jwt public key",
				Issuer:     "issuer",
				Audience:   "audience",
				Algorithm:  *jwt.SigningMethodRS256,
				ExpiresIn:  15 * time.Minute,
			},
			Database: mongo.Configuration{
				URL:          "mongodb://test:test@localhost:27020/",
				DatabaseName: "db",
			},
		},
	}

	t.Setenv("GRPC_URL", defaultConfiguration.GrpcURL)
	t.Setenv("GRPC_DOMAIN", defaultConfiguration.GrpcDomain)

	t.Setenv("JWT_PUBLIC_KEY", defaultConfiguration.ExternalCfg.JwtCfg.PublicKey)
	t.Setenv("JWT_PRIVATE_KEY", defaultConfiguration.ExternalCfg.JwtCfg.PrivateKey)
	t.Setenv("JWT_ISSUER", defaultConfiguration.ExternalCfg.JwtCfg.Issuer)
	t.Setenv("JWT_AUDIENCE", defaultConfiguration.ExternalCfg.JwtCfg.Audience)

	t.Setenv("KAFKA_BROKERS", "localhost:8080,localhost:8081")
	t.Setenv("KAFKA_TOPIC_TARIFF", defaultConfiguration.KafkaTopicTariff)

	t.Setenv("AUTH_MICROSERVICE_URL", defaultConfiguration.AuthMicroserviceURL)
	t.Setenv("HUBADMIN_MICROSERVICE_URL", defaultConfiguration.HubadminMicroserviceURL)
	t.Setenv("CURRENCY_MICROSERVICE_URL", defaultConfiguration.CurrencyMicroserviceURL)
	t.Setenv("DISCOUNT_MICROSERVICE_GRPC_URL", defaultConfiguration.DiscountMicroserviceGRPC)
	t.Setenv("PAYMENT_MICROSERVICE_GRPC_URL", defaultConfiguration.PaymentMicroserviceGRPC)
	t.Setenv("VERIFICATION_MICROSERVICE_URL", defaultConfiguration.VerificationMicroservice)
	t.Setenv("TEMPLATEGEN_MICROSERVICE_URL", defaultConfiguration.TemplategenMicroserviceURL)

	t.Setenv("MONGO_URL", defaultConfiguration.ExternalCfg.Database.URL)
	t.Setenv("MONGO_DB_NAME", defaultConfiguration.ExternalCfg.Database.DatabaseName)

	return &defaultConfiguration
}
