package ai

import (
	"fmt"
	"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 IslanderChatStreamHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.IslanderChatReq
		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
		}

		l := ai.NewIslanderChatStreamLogic(r.Context(), svcCtx)
		stream, err := l.ChatStream(&req)
		if err != nil {
			response.Write(r.Context(), w, nil, err)
			return
		}

		flusher, ok := w.(http.Flusher)
		if !ok {
			response.Write(r.Context(), w, nil, response.WrapRequestError(fmt.Errorf("server does not support streaming")))
			return
		}

		w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
		w.Header().Set("Cache-Control", "no-cache")
		w.Header().Set("Connection", "keep-alive")
		w.Header().Set("X-Accel-Buffering", "no")

		for event := range stream {
			writeSSE(w, event.Type, event)
			flusher.Flush()
			if event.Type == "done" || event.Type == "error" {
				return
			}
		}
		writeSSE(w, "done", map[string]string{"finish_reason": "stop"})
		flusher.Flush()
	}
}
