package routes

import (
	"net"
	"net/http"

	"github.com/gin-gonic/gin"

	"github.com/pardnchiu/agenvoy/internal/routes/handler"
	completionsHandler "github.com/pardnchiu/agenvoy/internal/routes/handler/chatCompletions"
)

func New() *gin.Engine {
	gin.SetMode(gin.ReleaseMode)

	r := gin.New()
	r.Use(gin.Recovery())

	r.POST("/v1/chat/completions", completionsHandler.ChatCompletions())
	r.POST("/v1/send", handler.Send())

	r.GET("/v1/tools", handler.ListTools())
	r.POST("/v1/tool/:tool_name", handler.CallTool())
	r.GET("/v1/session/:session_id/status", handler.GetSessionStatus())
	r.GET("/v1/session/:session_id/log", handler.StreamSessionLog())
	r.POST("/v1/session/:session_id/event", localhostOnly(), handler.PublishSessionEvent())
	r.GET("/v1/page/listener/:session_id", handler.PageListener())
	r.GET("/:session_id/*filepath", handler.PageView())
	r.GET("/v1/key", localhostOnly(), handler.GetKey())

	return r
}

func localhostOnly() gin.HandlerFunc {
	return func(c *gin.Context) {
		host, _, err := net.SplitHostPort(c.Request.RemoteAddr)
		if err != nil {
			host = c.Request.RemoteAddr
		}
		switch host {
		case "127.0.0.1", "::1":
			c.Next()
		default:
			c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": gin.H{"message": "localhost only", "type": "forbidden"}})
		}
	}
}
