package handler

import (
	"net/http"
	"os"

	"github.com/zs/InsightReply/internal/service"
)

type AIHandler struct {
	svc *service.AIService
}

func NewAIHandler(svc *service.AIService) *AIHandler {
	return &AIHandler{svc: svc}
}

func (h *AIHandler) Test(w http.ResponseWriter, r *http.Request) {
    // ...
}

func (h *AIHandler) Generate(w http.ResponseWriter, r *http.Request) {
	var body struct {
		TweetContent string `json:"tweet_content"`
		Strategy     string `json:"strategy"`
		Identity     string `json:"identity"`
	}

	if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
		SendError(w, http.StatusBadRequest, 4001, "Invalid request body")
		return
	}

	if body.TweetContent == "" {
		SendError(w, http.StatusBadRequest, 4002, "Tweet content is required")
		return
	}

	ctx := r.Context()
	reply, err := h.svc.GenerateReply(ctx, body.TweetContent, body.Strategy, body.Identity)
	if err != nil {
		SendError(w, http.StatusBadGateway, 5002, "Failed to generate AI reply: "+err.Error())
		return
	}

	SendSuccess(w, map[string]string{
		"reply": reply,
	})
}
