package v1 import ( "net/http" "github.com/gin-gonic/gin" "github.com/gochat/gochat/internal/service" "github.com/gochat/gochat/pkg/response" ) // InboxCsatTemplateHandler handles CSAT template endpoints for inboxes. // Reference: Chatwoot app/controllers/api/v1/accounts/inboxes/inbox_csat_templates_controller.rb // CSAT template is a singular resource per inbox: show, create, and analyze actions. type InboxCsatTemplateHandler struct { svc *service.CsatTemplateService } // NewInboxCsatTemplateHandler creates a new InboxCsatTemplateHandler. func NewInboxCsatTemplateHandler(svc *service.CsatTemplateService) *InboxCsatTemplateHandler { return &InboxCsatTemplateHandler{svc: svc} } // Show retrieves the CSAT template status for an inbox. // GET /api/v1/accounts/:account_id/inboxes/:inbox_id/csat_template // If no template exists, returns an empty object (Chatwoot returns nil in this case). func (h *InboxCsatTemplateHandler) Show(c *gin.Context) { inboxID, err := parseUintParam(c, "inbox_id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox_id") return } template, svcErr := h.svc.ShowTemplateStatus(c.Request.Context(), inboxID) if svcErr != nil { handleServiceError(c, svcErr) return } // If no template exists for this inbox, return empty object (Chatwoot pattern) if template == nil { response.OK(c, gin.H{}) return } response.OK(c, template) } // Create creates or updates a CSAT template for an inbox. // POST /api/v1/accounts/:account_id/inboxes/:inbox_id/csat_template // Body: { "message": "..." } func (h *InboxCsatTemplateHandler) Create(c *gin.Context) { inboxID, err := parseUintParam(c, "inbox_id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox_id") return } var req service.CreateCsatTemplateRequest if err := c.ShouldBindJSON(&req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid request body") return } template, svcErr := h.svc.CreateTemplate(c.Request.Context(), inboxID, req) if svcErr != nil { handleServiceError(c, svcErr) return } response.OK(c, template) } // Analyze analyzes a CSAT template message for quality. // POST /api/v1/accounts/:account_id/inboxes/:inbox_id/csat_template/analyze // Body: { "message": "..." } func (h *InboxCsatTemplateHandler) Analyze(c *gin.Context) { inboxID, err := parseUintParam(c, "inbox_id") if err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox_id") return } var req service.AnalyzeCsatTemplateRequest if err := c.ShouldBindJSON(&req); err != nil { response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid request body") return } result, svcErr := h.svc.AnalyzeTemplate(c.Request.Context(), inboxID, req) if svcErr != nil { handleServiceError(c, svcErr) return } response.OK(c, result) }