// Code generated by candi v1.3.1.

package usecase

import (
	"context"
	"time"

	"monorepo/services/notification-service/internal/modules/push-notif/domain"
	"monorepo/services/notification-service/pkg/shared/repository"

	"pkg.agungdp.dev/candi/codebase/factory/dependency"
	"pkg.agungdp.dev/candi/codebase/interfaces"
	"pkg.agungdp.dev/candi/logger"
	"pkg.agungdp.dev/candi/tracer"
)

type pushnotifUsecaseImpl struct {
	cache interfaces.Cache

	repoMongo *repository.RepoMongo

	// for subscriber listener
	eventChannelSubscribers map[string]chan<- *domain.Event
}

// NewPushNotifUsecase usecase impl constructor
func NewPushNotifUsecase(deps dependency.Dependency) PushNotifUsecase {
	return &pushnotifUsecaseImpl{
		cache: deps.GetRedisPool().Cache(),

		repoMongo:               repository.GetSharedRepoMongo(),
		eventChannelSubscribers: make(map[string]chan<- *domain.Event),
	}
}

func (uc *pushnotifUsecaseImpl) Hello(ctx context.Context) (msg string) {
	trace := tracer.StartTrace(ctx, "PushNotifUsecase:Hello")
	defer trace.Finish()
	ctx = trace.Context()

	msg, _ = uc.repoMongo.PushNotifRepo.FindHello(ctx)
	return
}

func (uc *pushnotifUsecaseImpl) SendNotification(ctx context.Context, request *domain.PushNotifRequestPayload) (err error) {
	trace := tracer.StartTrace(ctx, "Usecase-SendNotification")
	defer trace.Finish()
	ctx = trace.Context()

	// requestPayload := domain.PushRequest{
	// 	To: request.To,
	// 	Notification: &domain.Notification{
	// 		Title:          request.Title,
	// 		Body:           request.Message,
	// 		Image:          "https://storage.googleapis.com/agungdp/static/logo/golang.png",
	// 		Sound:          "default",
	// 		MutableContent: true,
	// 		ResourceID:     "resourceID",
	// 		ResourceName:   "resourceName",
	// 	},
	// 	Data: map[string]interface{}{"type": "type"},
	// }

	// send to internal subscriber
	// go uc.PublishMessageToTopic(ctx, &domain.Event{Topic: requestPayload.To})

	// result := <-uc.repoMongo.PushNotif.Push(ctx, requestPayload)
	// if result.Error != nil {
	// 	return result.Error
	// }

	logger.LogI("success send notification")
	return
}

func (uc *pushnotifUsecaseImpl) SendScheduledNotification(ctx context.Context, scheduledAt time.Time, request *domain.PushNotifRequestPayload) (err error) {
	trace := tracer.StartTrace(ctx, "Usecase-SendScheduledNotification")
	defer trace.Finish()
	ctx = trace.Context()
	return nil

	// redisTopicKey := candihelper.BuildRedisPubSubKeyTopic(string(uc.modName), "scheduled-push-notif")
	// data, _ := json.Marshal(request)
	// exp := scheduledAt.Sub(time.Now())
	// return uc.repoMongo.Schedule.SaveScheduledNotification(ctx, redisTopicKey, data, exp)
}

func (uc *pushnotifUsecaseImpl) SendScheduledEvent(ctx context.Context, scheduledAt time.Time, request *domain.Event) (err error) {
	trace := tracer.StartTrace(ctx, "Usecase-SendScheduledEvent")
	defer trace.Finish()
	ctx = trace.Context()
	return nil

	// redisTopicKey := candihelper.BuildRedisPubSubKeyTopic(string(uc.modName), "broadcast-topic")
	// data, _ := json.Marshal(request)
	// exp := scheduledAt.Sub(time.Now())
	// return uc.repoMongo.Schedule.SaveScheduledNotification(ctx, redisTopicKey, data, exp)
}
