package main

import (
	"fmt"
	"log"
	"log/slog"
	"net/http"
	"os"
	"time"

	"gitea.henriburau.de/haw-lan/cod4watcher/models"
	"gitea.henriburau.de/haw-lan/cod4watcher/routes"
	"github.com/go-chi/chi"
	"github.com/joho/godotenv"
)

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

	cod4server, err := models.NewCOD4ServerStatus("80.57.28.137", "28960", 1*time.Second)
	if err != nil {
		log.Panic(err)
	}

	status, err := cod4server.GetServerStatus()
	if err != nil {
		log.Panic(err)
	}

	log.Printf("%+v", status)

	router := chi.NewMux()

	server := &routes.Server{}

	router.Handle("/*", public())
	router.Get("/health", routes.Make(server.HandleHealth))
	router.Get("/captures/{captureID}", routes.Make(server.HandleCapture))
	router.Get("/", routes.Make(server.HandleHome))

	listenAddr := os.Getenv("LISTEN_ADDR")
	slog.Info("HTTP server started", "listenAddr", listenAddr)
	http.ListenAndServe(listenAddr, router)
}

func public() http.Handler {
	fmt.Println("building static files for development")
	return http.StripPrefix("/assets/", http.FileServerFS(os.DirFS("assets")))
}
