package initialize

import (
	"go.uber.org/zap"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/account"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/broker/tariff"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/callback"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/cart"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/currency"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/history"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/payment"
	"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/service/wallet"
)

type ServicesDeps struct {
	Logger            *zap.Logger
	Repositories      *Repositories
	Clients           *Clients
	Brokers           *Brokers
	ConfigurationGRPC *models.ConfigurationGRPC
}

type Services struct {
	AccountService         *account.Service
	CurrencyService        *currency.Service
	CartService            *cart.Service
	HistoryService         *history.Service
	WalletService          *wallet.Service
	PaymentService         *payment.Service
	PaymentCallbackService *callback.PaymentCallbackService
	TariffBrokerService    *tariff.Service
}

func NewServices(deps ServicesDeps) *Services {
	historyService := history.New(history.Deps{
		Logger:     deps.Logger,
		Repository: deps.Repositories.HistoryRepository,
		AuthClient: deps.Clients.AuthClient,
	})

	walletService := wallet.New(wallet.Deps{
		Logger:         deps.Logger,
		Repository:     deps.Repositories.AccountRepository,
		CurrencyClient: deps.Clients.CurrencyClient,
		HistoryService: historyService,
	})

	tariffBrokerService := tariff.New(tariff.Deps{
		Logger:   deps.Logger,
		Consumer: deps.Brokers.TariffConsumer,
		Producer: deps.Brokers.TariffProducer,
	})

	return &Services{
		WalletService:  walletService,
		HistoryService: historyService,
		AccountService: account.New(account.Deps{
			Logger:     deps.Logger,
			Repository: deps.Repositories.AccountRepository,
			AuthClient: deps.Clients.AuthClient,
		}),
		CurrencyService: currency.New(currency.Deps{
			Logger:     deps.Logger,
			Repository: deps.Repositories.CurrencyRepository,
		}),
		CartService: cart.New(cart.Deps{
			Logger:              deps.Logger,
			Repository:          deps.Repositories.AccountRepository,
			HubadminClient:      deps.Clients.HubadminClient,
			DiscountClient:      deps.Clients.DiscountClient,
			WalletService:       walletService,
			HistoryService:      historyService,
			TariffBrokerService: tariffBrokerService,
		}),
		PaymentService: payment.New(payment.Deps{
			Logger:            deps.Logger,
			ConfigurationGRPC: deps.ConfigurationGRPC,
			PaymentClient:     deps.Clients.PaymentClient,
			AuthClient:        deps.Clients.AuthClient,
		}),
		PaymentCallbackService: callback.NewPaymentCallbackService(callback.PaymentCallbackServiceDeps{
			Logger:            deps.Logger,
			AccountRepository: deps.Repositories.AccountRepository,
			WalletService:     walletService,
			HistoryService:    historyService,
		}),
		TariffBrokerService: tariffBrokerService,
	}
}
