// Package main provides a utility to dump raw RIS Live stream messages to stdout
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"

	"git.eeqj.de/sneak/routewatch/internal/logger"
	"git.eeqj.de/sneak/routewatch/internal/metrics"
	"git.eeqj.de/sneak/routewatch/internal/streamer"
)

func main() {
	// Set up logger
	logger := logger.New()

	// Create metrics tracker
	metricsTracker := metrics.New()

	// Create streamer
	s := streamer.New(logger, metricsTracker)

	// Register raw message handler that prints to stdout
	s.RegisterRawHandler(func(line string) {
		fmt.Println(line)
	})

	// Set up context with cancellation
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// Handle signals
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-sigChan
		log.Println("Received shutdown signal")
		cancel()
	}()

	// Start streaming
	if err := s.Start(); err != nil {
		log.Fatal("Failed to start streamer:", err)
	}
	defer s.Stop()

	// Wait for context cancellation
	<-ctx.Done()
}
