package main

import (
	"fmt"
	"golang.source-fellows.com/pattern/saga/internal"
)

func main() {
	conn := internal.ConnectToBroker()
	defer conn.Close()

	ch, err := conn.Channel()
	internal.FailOnError(err, "Failed to open a channel")
	defer ch.Close()

	// Queues erstellen
	queues := []string{internal.NewRegistrationQueueName, internal.RegistrationReplyQueueName, internal.CustomerNotificationQueueName}
	for _, queue := range queues {
		_, err = ch.QueueDeclare(
			queue,
			true,
			false,
			false,
			false,
			nil,
		)
		internal.FailOnError(err, fmt.Sprintf("Failed to declare %s queue", queue))
	}

	// Starte Services
	go internal.StartRegistrationService(ch)
	go internal.StartAdministrationService(ch)
	go internal.StartNotificationService(ch)

	internal.SendRegistration(ch)

	// Warte auf Beendigung
	select {}
}
