package main

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

	"krates/server/internal/api"
	"krates/server/internal/crdt"
	"krates/server/internal/ws"
)

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}

	crdtProvider := crdt.NewProvider()

	wsManager := ws.NewManager(crdtProvider)

	mux := http.NewServeMux()
	mux.Handle("/ws/shell", wsManager.WithWebSocket(ws.ShellHandler))
	mux.Handle("/ws/logs", wsManager.WithWebSocket(ws.LogsHandler))
	mux.Handle("/ws/watch", wsManager.WithWebSocket(ws.WatchHandler))
	mux.Handle("/ws/sync", wsManager.WithWebSocket(ws.SyncHandler))

	apiRoutes := api.SetupRoutes(wsManager)
	mux.Handle("/api/", http.StripPrefix("/api", apiRoutes))

	http.ListenAndServe(":"+port, mux)
	log.Printf("Server started on port %s", port)
}
