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) {
	metricLabels := dc.usageLabels(s, responseModeNormalized)
	text, reasoning, _, _, usage, _, err := collectRunResult(dc.r.Context(), handle.Stream(), handle.WaitTimeout())
	if err != nil {
		s.cancelRunOnHTTPGiveUp(handle.Dispatch(), err)
		emitUsageMetrics(metricLabels, usageStatusForError(err), usageObservation{})
		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
	}

	emitUsageMetrics(metricLabels, usageStatusSuccess, usageObservationFromOpenAIUsage(usage, len(reasoning)))

	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: []responsesOutputItem{{
			Type: "message",
			Role: "assistant",
			Content: []responsesContentItem{{
				Type: "output_text",
				Text: text,
			}},
		}},
		Usage: u,
	})
}
