package internal

import (
	"encoding/json"
	"fmt"
	"github.com/streadway/amqp"
	"golang.source-fellows.com/pattern/saga/internal/model"
)

func StartNotificationService(ch *amqp.Channel) {
	msgs, err := ch.Consume(
		CustomerNotificationQueueName,
		"",
		true,
		false,
		false,
		false,
		nil,
	)
	FailOnError(err, "Failed to register consumer")

	go func() {
		for d := range msgs {
			customerNotification := model.CustomerNotification{}
			err := json.Unmarshal(d.Body, &customerNotification)
			FailOnError(err, "could not unmarshal json")

			fmt.Printf("NotificationService: Received CustomerNotification %+v\n", customerNotification)

			// Simuliere Benachrichtigung
			fmt.Printf("NotificationService: Registration %s has been completed successfully!\n", customerNotification)
		}
	}()
}
