package svc

import (
	"backend/internal/config"
	errs "backend/pkg/library/errors"
	"backend/pkg/notification/domain/usecase"
	uc "backend/pkg/notification/usecase"

	cfg "backend/pkg/notification/config"
	"backend/pkg/notification/domain/repository"
	rp "backend/pkg/notification/repository"
)

func MustSMTPEmailSender(c *config.Config) repository.MailRepository {
	return rp.MustSMTPUseCase(rp.SMTPMailUseCaseParam{
		Conf: cfg.SMTPConfig{
			Enable:           c.SMTPConfig.Enable,
			Sort:             1,
			GoroutinePoolNum: c.SMTPConfig.GoroutinePoolNum,
			Host:             c.SMTPConfig.Host,
			Port:             c.SMTPConfig.Port,
			Username:         c.SMTPConfig.Username,
			Password:         c.SMTPConfig.Password,
		},
	})
}

func MustAwsEmailSender(c *config.Config) repository.MailRepository {
	return rp.MustAwsSesMailRepository(rp.AwsEmailDeliveryParam{
		Conf: &cfg.AmazonSesSettings{},
	})
}

func MustSMS(c *config.Config) repository.SMSClientRepository {
	return rp.MustMitakeRepository(rp.MitakeSMSDeliveryParam{
		Conf: &cfg.MitakeSMSSender{},
	})
}

func MustDeliveryUseCase(c *config.Config, logger errs.Logger) usecase.DeliveryUseCase {
	emailProviders := make([]usecase.EmailProvider, 0, 10)
	smsProviders := make([]usecase.SMSProvider, 0)
	//smsProviders = append(smsProviders, usecase.SMSProvider{
	//	Sort: 1,
	//	Repo: MustSMS(c),
	//})

	//emailProviders = append(emailProviders, usecase.EmailProvider{
	//	Sort: 2,
	//	Repo: MustAwsEmailSender(c),
	//})

	emailProviders = append(emailProviders, usecase.EmailProvider{
		Sort: 1,
		Repo: MustSMTPEmailSender(c),
	})

	return uc.MustDeliveryUseCase(uc.DeliveryUseCaseParam{
		SMSProviders:   smsProviders,
		EmailProviders: emailProviders,
		DeliveryConfig: cfg.DeliveryConfig{
			MaxRetries:    c.DeliveryConfig.MaxRetries,
			InitialDelay:  c.DeliveryConfig.InitialDelay,
			BackoffFactor: c.DeliveryConfig.BackoffFactor,
			MaxDelay:      c.DeliveryConfig.MaxDelay,
			Timeout:       c.DeliveryConfig.Timeout,
			EnableHistory: false,
		},
		Logger: logger,
	})
}
