package handlers import ( "net/http" "git.linuxforward.com/byom/byom-trends/common" "git.linuxforward.com/byom/byom-trends/logger" "git.linuxforward.com/byom/byom-trends/services/suggestions" "git.linuxforward.com/byom/byom-trends/store" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type SuggestionsHandler struct { store *store.DataStore analyzer *suggestions.Analyzer logger *logger.Entry } func NewSuggestionsHandler(store *store.DataStore, analyzer *suggestions.Analyzer) *SuggestionsHandler { return &SuggestionsHandler{ store: store, analyzer: analyzer, logger: logger.NewLogger("suggestions-handler"), } } func (h *SuggestionsHandler) GetSuggestions(c *gin.Context) { var req common.GetSuggestionsRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format"}) return } var trends []common.ContentTrend for _, social := range req.Socials { socialTrends, err := h.store.GetTrendsByUsername(c, social.Username) if err != nil { h.logger.WithError(err).Error("failed to get trends") c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get trends"}) return } trends = append(trends, socialTrends...) } suggestions, err := h.analyzer.AnalyzeTrendsForProducts(trends) if err != nil { h.logger.WithError(err).Error("failed to analyze trends") c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to analyze trends"}) return } if err := h.store.SaveSuggestions(c, req.UserID, suggestions); err != nil { h.logger.WithError(err).Error("failed to save suggestions") c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save suggestions"}) return } c.JSON(http.StatusOK, suggestions) } func (h *SuggestionsHandler) GetUserSuggestions(c *gin.Context) { userID := c.Param("user_id") if userID == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "User ID is required"}) return } suggestions, err := h.store.GetSuggestionsByUserID(c, userID) if err != nil { h.logger.WithError(err).Error("failed to get suggestions") c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get suggestions"}) return } c.JSON(http.StatusOK, suggestions) } func (h *SuggestionsHandler) GetSuggestion(c *gin.Context) { id, err := uuid.Parse(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid suggestion ID"}) return } suggestion, err := h.store.GetSuggestionByID(c, id) if err != nil { h.logger.WithError(err).Error("failed to get suggestion") c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get suggestion"}) return } if suggestion == nil { c.JSON(http.StatusNotFound, gin.H{"error": "Suggestion not found"}) return } c.JSON(http.StatusOK, suggestion) }