package routes

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"scrum-solitare/src/handlers"
	"scrum-solitare/src/middleware"
)

func Register(r *gin.Engine, pages *handlers.PageHandler, rooms *handlers.RoomAPIHandler) {
	registerStatic(r)
	registerPages(r, pages)
	registerAPI(r, rooms)
}

func registerStatic(r *gin.Engine) {
	static := r.Group("/static")
	static.Use(middleware.StaticCacheControl())
	static.StaticFS("/", http.Dir("static"))
}

func registerPages(r *gin.Engine, pages *handlers.PageHandler) {
	r.GET("/", pages.ShowConfigPage)
	r.GET("/room/:roomID", pages.ShowRoomPage)
}

func registerAPI(r *gin.Engine, rooms *handlers.RoomAPIHandler) {
	api := r.Group("/api")
	{
		api.POST("/rooms", rooms.CreateRoom)
		api.POST("/rooms/:roomID/join", rooms.JoinRoom)
		api.GET("/rooms/:roomID/events", rooms.StreamEvents)
		api.POST("/rooms/:roomID/vote", rooms.CastVote)
		api.POST("/rooms/:roomID/reveal", rooms.RevealVotes)
		api.POST("/rooms/:roomID/reset", rooms.ResetVotes)
		api.POST("/rooms/:roomID/leave", rooms.LeaveRoom)
	}
}
