package server

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"scrabble/backend/internal/engine"
)

// handleDictBytes streams the raw serialized dictionary for a (variant, version)
// pair so the client can validate and score moves locally — the local move
// preview — instead of a network round trip per tile arrangement. It is reached
// only through the gateway's session-gated /dict route (which resolves the
// X-User-ID this group requires), and served with a long immutable cache lifetime
// because a published dictionary version never changes. An unknown variant or a
// version that is not resident is a 404.
func (s *Server) handleDictBytes(c *gin.Context) {
	variant, err := engine.ParseVariant(c.Param("variant"))
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "unknown variant"})
		return
	}
	data, err := s.games.DictBytes(variant, c.Param("version"))
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "dictionary not found"})
		return
	}
	c.Header("Cache-Control", "public, max-age=31536000, immutable")
	c.Data(http.StatusOK, "application/octet-stream", data)
}
