package initialize

import (
	"context"
	"crypto/tls"
	"gitea.pena/PenaSide/customer/pkg/customer_clients"
	"gitea.pena/SQuiz/common/clients"
	"gitea.pena/SQuiz/worker/internal/clients/gigachat"
	"gitea.pena/SQuiz/worker/internal/senders"
	"github.com/go-redis/redis/v8"
	"github.com/go-resty/resty/v2"
	"go.uber.org/zap"
)

type Clients struct {
	MailClient     *clients.SmtpClient
	CustomerClient *customer_clients.CustomersClient
	GigaChatClient *gigachat.GigaChatClient
}

func NewClients(ctx context.Context, cfg Config, logger *zap.Logger, redisClient *redis.Client) (*Clients, error) {
	notifyTgClient, err := senders.NewTgSender(cfg.NotifyTelegramToken)
	if err != nil {
		return nil, err
	}

	gigaChatClient, err := gigachat.NewGigaChatClient(ctx, gigachat.Deps{
		Logger:      logger,
		Client:      resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}),
		BaseURL:     cfg.GigaChatApiBaseURL,
		AuthKey:     cfg.GigaChatApiAuthKey,
		RedisClient: redisClient,

		TgSender: notifyTgClient,
		TgChatID: cfg.NotifyChannelID,
	})
	if err != nil {
		return nil, err
	}

	return &Clients{
		MailClient: clients.NewSmtpClient(clients.Deps{
			SmtpSender: cfg.Sender,
			ApiKey:     cfg.ApiKey,
			SmtpApiUrl: cfg.ApiUrl,
		}),
		CustomerClient: customer_clients.NewCustomersClient(customer_clients.CustomersClientDeps{
			Logger:              logger,
			CustomerServiceHost: cfg.CustomerMicroserviceRPCURL,
		}),
		GigaChatClient: gigaChatClient,
	}, nil
}
