package main

import (
	"context"
	"errors"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/joho/godotenv"
	"github.com/speedrun-website/leaderboard-backend/database"
	"github.com/speedrun-website/leaderboard-backend/server"
)

func main() {
	if err := godotenv.Load(os.Getenv("ENV")); err != nil {
		log.Fatal(err)
	}

	if err := database.InitGlobalConnection(); err != nil {
		log.Fatal(err)
	}

	r := gin.Default()
	server.Init(r)
	port := os.Getenv("BACKEND_PORT")
	srv := &http.Server{
		Addr:    ":" + port,
		Handler: r,
	}
	go func() {
		err := srv.ListenAndServe()
		if err != nil {
			if errors.Is(err, http.ErrServerClosed) {
				log.Print("Server shutdown complete!")
			} else {
				log.Printf("Server encountered unexpected error: %s", err)
			}
		}
	}()

	// Wait for interrupt signal to gracefully shutdown
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt)
	<-quit
	log.Println("Interrupt received. Server shutting down...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := srv.Shutdown(ctx); err != nil {
		log.Printf("Server forced to shutdown: %s", err)
	}

	log.Println("Exiting")
}
