package main

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

	"github.com/hexahost/gamecloud/edge-gateway/internal/config"
	"github.com/hexahost/gamecloud/edge-gateway/internal/proxy"
)

func main() {
	log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
		Level: slog.LevelInfo,
	}))

	enabled := os.Getenv("EDGE_GATEWAY_ENABLED")
	if enabled != "true" && enabled != "1" {
		log.Info("edge gateway disabled",
			"feature", "edge-gateway",
			"hint", "set EDGE_GATEWAY_ENABLED=true to start the TCP join-to-start gateway",
		)
		return
	}

	cfg := config.Load()
	gateway := proxy.New(cfg, log)

	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer stop()

	if err := gateway.ListenAndServe(ctx); err != nil {
		log.Error("edge gateway stopped", "error", err)
		os.Exit(1)
	}
}
