package api

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/paramah/ai_devs4/s01e03/internal/usecase"
)

// Handler handles HTTP requests
type Handler struct {
	conversationUC *usecase.ConversationUseCase
}

// NewHandler creates a new API handler
func NewHandler(conversationUC *usecase.ConversationUseCase) *Handler {
	return &Handler{
		conversationUC: conversationUC,
	}
}

// ShadowRequest represents the request for /shadow endpoint
type ShadowRequest struct {
	SessionID string `json:"sessionID" binding:"required"`
	Msg       string `json:"msg" binding:"required"`
}

// ShadowResponse represents the response for /shadow endpoint
type ShadowResponse struct {
	Msg string `json:"msg"`
}

// Shadow handles the /shadow endpoint
func (h *Handler) Shadow(c *gin.Context) {
	var req ShadowRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// Process message
	response, err := h.conversationUC.ProcessMessage(c.Request.Context(), req.SessionID, req.Msg)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, ShadowResponse{
		Msg: response,
	})
}
