package openai

import (
	"io"
	"net/http"

	edgeservice "iop/apps/edge/internal/service"
)

func (s *Server) handleOllamaAPI(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet && r.Method != http.MethodPost && r.Method != http.MethodDelete {
		writeError(w, http.StatusMethodNotAllowed, "method_not_allowed", "method not allowed")
		return
	}
	defer r.Body.Close()
	body, err := io.ReadAll(io.LimitReader(r.Body, 16<<20))
	if err != nil {
		writeError(w, http.StatusBadRequest, "invalid_request_error", "read request body failed")
		return
	}
	resp, err := s.service.OllamaAPI(r.Context(), edgeservice.OllamaAPIRequest{
		NodeRef:    s.cfg.NodeRef,
		Adapter:    s.resolveAdapter(),
		Target:     s.resolveTarget(""),
		Method:     r.Method,
		Path:       r.URL.RequestURI(),
		Body:       string(body),
		TimeoutSec: s.resolveTimeoutSec(),
	})
	if err != nil {
		writeError(w, http.StatusBadGateway, "ollama_passthrough_error", err.Error())
		return
	}
	contentType := resp.ContentType
	if contentType == "" {
		contentType = "application/json"
	}
	w.Header().Set("Content-Type", contentType)
	status := resp.StatusCode
	if status < 100 || status > 999 {
		status = http.StatusOK
	}
	w.WriteHeader(status)
	_, _ = w.Write([]byte(resp.Body))
}
