package main

import (
	"encoding/json"
	"fmt"
	cloudevents "github.com/cloudevents/sdk-go/v2"
	"github.com/google/uuid"
	"github.com/nats-io/nats.go"
	nats2 "golang.source-fellows.com/training-fellow/booking/internal/inbound/nats"
	"log"
)

func main() {

	nc, err := nats.Connect("localhost:4222")
	if err != nil {
		log.Fatal(err)
	}

	for i := range 10 {

		training := nats2.Training{
			Name:         fmt.Sprintf("test-%d", i),
			NumberOfDays: 2,
		}

		bytes, err := json.Marshal(training)
		if err != nil {
			log.Fatal(err)
		}

		event := cloudevents.NewEvent()
		event.SetID(uuid.New().String())
		event.SetSource("source-fellows.com/trainingfellow/admin")
		event.SetType("source-fellows.com/trainingfellow/training")
		event.SetData(cloudevents.ApplicationJSON, bytes)

		data, err := event.MarshalJSON()
		if err != nil {
			log.Fatal(err)
		}

		err = nc.Publish(nats2.SubjectNameNewTrainings, data)
		if err != nil {
			log.Fatal(err)
		}
		nc.Flush()
	}

}
