package ai

import (
	"net/http"

	"haixun-backend/internal/logic/ai"
	"haixun-backend/internal/response"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

	"github.com/zeromicro/go-zero/rest/httpx"
)

func ChatHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.AIChatReq
		if err := httpx.Parse(r, &req); err != nil {
			response.Write(r.Context(), w, nil, response.WrapRequestError(err))
			return
		}
		if err := svcCtx.Validator.ValidateAll(&req); err != nil {
			response.Write(r.Context(), w, nil, response.WrapRequestError(err))
			return
		}

		token, err := ai.BearerToken(r)
		if err != nil {
			response.Write(r.Context(), w, nil, err)
			return
		}

		l := ai.NewChatLogic(r.Context(), svcCtx)
		data, err := l.Chat(&req, token)
		response.Write(r.Context(), w, data, err)
	}
}
