// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2

package svc

import (
	"time"

	"juwan-backend/app/email/api/internal/config"
	"juwan-backend/app/email/api/internal/middleware"
	"juwan-backend/common/redisx"

	"github.com/redis/go-redis/v9"
	"github.com/zeromicro/go-queue/kq"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/rest"
)

type ServiceContext struct {
	Config       config.Config
	Logger       rest.Middleware
	RedisCluster *redis.ClusterClient
	EmailPusher  *kq.Pusher
}

func NewServiceContext(c config.Config) *ServiceContext {
	redisConn, err := redisx.ConnectMasterSlaveCluster(c.CacheConf, 5*time.Second)
	if err != nil {
		logx.Errorf("failed to connect redis for email-api: %v", err)
	}

	var emailPusher *kq.Pusher
	if len(c.Kmq.Brokers) > 0 && c.Kmq.Topic != "" {
		emailPusher = kq.NewPusher(c.Kmq.Brokers, c.Kmq.Topic)
	}

	return &ServiceContext{
		Config:       c,
		Logger:       middleware.NewLoggerMiddleware().Handle,
		RedisCluster: redisConn.Client,
		EmailPusher:  emailPusher,
	}
}
