package openai

import (
	"go.uber.org/zap"
	edgeservice "iop/apps/edge/internal/service"
	"net/http"
	"time"
)

func (s *Server) completeResponse(w http.ResponseWriter, dc *responsesDispatchContext, handle edgeservice.RunResult) {
	text, reasoning, _, toolCalls, usage, _, err := collectRunResult(dc.r.Context(), handle.Stream(), handle.WaitTimeout())
	if err != nil {
		s.cancelRunOnHTTPGiveUp(handle.Dispatch(), err)
		dc.finishUsageRequest(usageStatusForError(err), responseModeNormalized)
		writeError(w, httpStatusForRunError(err), "run_error", err.Error())
		return
	}
	text, reasoning, normalized := normalizeCompletionOutput(dc.outputPolicy, text, reasoning, false)
	s.logger.Info("openai responses output",
		zap.String("run_id", handle.Dispatch().RunID),
		zap.Bool("strict_output", dc.outputPolicy.Strict),
		zap.String("xml_completion_tool", dc.outputPolicy.XMLCompletionTool),
		zap.Bool("normalized", normalized),
		zap.Int("content_len", len(text)),
		zap.Int("reasoning_len", len(reasoning)),
	)

	var u openAIUsage
	if usage != nil {
		u = *usage
	}

	dc.recordUsageAttempt(handle.Dispatch(), responseModeNormalized, usageObservationFromOpenAIUsage(usage, len(reasoning)))
	dc.finishUsageRequest(usageStatusSuccess, responseModeNormalized)

	writeJSON(w, http.StatusOK, responsesResponse{
		ID:         "resp-" + handle.Dispatch().RunID,
		Object:     "response",
		CreatedAt:  time.Now().Unix(),
		Model:      responseModel(dc.req.Model, handle.Dispatch().Target),
		OutputText: text,
		Output:     responsesOutputItems(text, toolCalls),
		Usage:      u,
	})
}
