package router

import (
	"ashareview-server/handler"
	"net/http"
	"os"
	"path/filepath"

	"github.com/gin-gonic/gin"
)

func Setup(r *gin.Engine, dh *handler.DashboardHandler) {
	api := r.Group("/api")
	{
		api.GET("/dashboard/summary", dh.GetSummary)
		api.GET("/dashboard/daily", dh.GetDailySummary)
		api.GET("/dashboard/consecutive", dh.GetConsecutiveLimits)
	}

	// Serve frontend static files
	staticDir := "static"
	if _, err := os.Stat(staticDir); os.IsNotExist(err) {
		// Try relative to executable
		exePath, _ := os.Executable()
		staticDir = filepath.Join(filepath.Dir(exePath), "static")
	}

	r.Static("/assets", filepath.Join(staticDir, "assets"))
	r.StaticFile("/vite.svg", filepath.Join(staticDir, "vite.svg"))

	// SPA fallback: all non-API routes serve index.html
	r.NoRoute(func(c *gin.Context) {
		path := c.Request.URL.Path
		if len(path) < 4 || path[:4] != "/api" {
			indexPath := filepath.Join(staticDir, "index.html")
			c.File(indexPath)
			return
		}
		c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
	})
}
