package initialize_test

import (
	"testing"
	"time"

	"github.com/golang-jwt/jwt/v5"
	"github.com/stretchr/testify/assert"
	"penahub.gitlab.yandexcloud.net/backend/penahub_common/mongo"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/initialize"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
)

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 := models.AuthMicroserviceURL{
		User: "http://www.auth.ru/user",
	}

	defaultHubAdminURL := models.HubadminMicroserviceURL{
		Tariff: "http://www.hubadmin.ru/tariff",
	}

	defaultCurrencyURL := models.CurrencyMicroserviceURL{
		Translate: "http://www.currency.ru/change",
	}

	defaultConfiguration := models.Config{
		HTTP: models.ConfigurationHTTP{
			ClientHost: "localhost",
			ClientPort: "8080",
			AdminHost:  "localhost",
			AdminPort:  "8081",
		},
		GRPC: models.ConfigurationGRPC{
			Host:  "localhost",
			Port:  "8081",
			Domen: "domen",
		},
		Service: models.ServiceConfiguration{
			AuthMicroservice: models.AuthMicroserviceConfiguration{
				URL: defaultAuthURL,
			},
			Kafka: models.KafkaConfiguration{
				Tariff:  models.TariffKafkaConfiguration{Topic: "topic"},
				Brokers: []string{"localhost:8080", "localhost:8081"},
			},
			HubadminMicroservice: models.HubadminMicroserviceConfiguration{
				URL: defaultHubAdminURL,
			},
			CurrencyMicroservice: models.CurrencyMicroserviceConfiguration{
				URL: defaultCurrencyURL,
			},
			DiscountMicroservice: models.DiscountMicroserviceConfiguration{
				HostGRPC: "domen",
			},
			PaymentMicroservice: models.PaymentMicroserviceConfiguration{
				HostGRPC: "domen",
			},
			VerificationMicroservice: models.VerificationMicroserviceConfiguration{
				URL: models.VerificationMicroserviceURL{
					Verification: "domen",
				},
			},
			TemplategenMicroserviceURL: models.TemplategenMicroserviceConfiguration{
				URL: models.TemplategenMicroserviceURL{
					Templategen: "domen",
				},
			},
			JWT: models.JWTConfiguration{
				PrivateKey: "jwt private key",
				PublicKey:  "jwt public key",
				Issuer:     "issuer",
				Audience:   "audience",
				Algorithm:  *jwt.SigningMethodRS256,
				ExpiresIn:  15 * time.Minute,
			},
		},
		Database: mongo.Configuration{
			Host:         "localhost",
			Port:         "27017",
			User:         "user",
			Password:     "pass",
			Auth:         "db",
			DatabaseName: "db",
		},
	}

	t.Setenv("GRPC_HOST", defaultConfiguration.GRPC.Host)
	t.Setenv("GRPC_PORT", defaultConfiguration.GRPC.Port)
	t.Setenv("GRPC_DOMEN", defaultConfiguration.GRPC.Domen)

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

	t.Setenv("KAFKA_BROKERS", "localhost:8080,localhost:8081")
	t.Setenv("KAFKA_TOPIC_TARIFF", defaultConfiguration.Service.Kafka.Tariff.Topic)

	t.Setenv("AUTH_MICROSERVICE_USER_URL", defaultConfiguration.Service.AuthMicroservice.URL.User)
	t.Setenv("HUBADMIN_MICROSERVICE_TARIFF_URL", defaultConfiguration.Service.HubadminMicroservice.URL.Tariff)
	t.Setenv("CURRENCY_MICROSERVICE_TRANSLATE_URL", defaultConfiguration.Service.CurrencyMicroservice.URL.Translate)
	t.Setenv("DISCOUNT_MICROSERVICE_GRPC_HOST", defaultConfiguration.Service.DiscountMicroservice.HostGRPC)
	t.Setenv("PAYMENT_MICROSERVICE_GRPC_HOST", defaultConfiguration.Service.PaymentMicroservice.HostGRPC)
	t.Setenv("VERIFICATION_MICROSERVICE_USER_URL", defaultConfiguration.Service.VerificationMicroservice.URL.Verification)
	t.Setenv("TEMPLATEGEN_MICROSERVICE_URL", defaultConfiguration.Service.TemplategenMicroserviceURL.URL.Templategen)

	t.Setenv("MONGO_HOST", defaultConfiguration.Database.Host)
	t.Setenv("MONGO_PORT", defaultConfiguration.Database.Port)
	t.Setenv("MONGO_USER", defaultConfiguration.Database.User)
	t.Setenv("MONGO_PASSWORD", defaultConfiguration.Database.Password)
	t.Setenv("MONGO_AUTH", defaultConfiguration.Database.Auth)
	t.Setenv("MONGO_DB_NAME", defaultConfiguration.Database.DatabaseName)

	return &defaultConfiguration
}
