package main

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

	"git.toki-labs.com/toki/alt/services/api/internal/config"
	"git.toki-labs.com/toki/alt/services/api/internal/socket"
)

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

	cfg := config.Load()
	server := socket.NewServer(cfg)

	if err := server.Start(ctx); err != nil {
		slog.Error("failed to start api socket server", "error", err)
		os.Exit(1)
	}
	slog.Info("api socket server started", "addr", fmt.Sprintf("%s:%d%s", cfg.Host, cfg.Port, cfg.SocketPath))

	<-ctx.Done()
	if err := server.Stop(); err != nil {
		slog.Error("failed to stop api socket server", "error", err)
		os.Exit(1)
	}
}
