package openai

import (
	"net/http"
	"strings"
	"time"
)

func (s *Server) routes() *http.ServeMux {
	mux := http.NewServeMux()
	mux.HandleFunc("/healthz", s.handleHealthz)
	mux.HandleFunc("/v1/models", s.handleModels)
	mux.HandleFunc("/v1/chat/completions", s.handleChatCompletions)
	mux.HandleFunc("/v1/responses", s.handleResponses)
	mux.HandleFunc("/api/", s.handleOllamaAPI)
	return mux
}

func (s *Server) handleHealthz(w http.ResponseWriter, _ *http.Request) {
	writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}

func (s *Server) handleModels(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		writeError(w, http.StatusMethodNotAllowed, "method_not_allowed", "method not allowed")
		return
	}
	var modelIDs []string
	if len(s.cfg.ModelRoutes) > 0 {
		for _, route := range s.cfg.ModelRoutes {
			id := strings.TrimSpace(route.Model)
			if id != "" && route.Target != "" {
				modelIDs = append(modelIDs, id)
			}
		}
	} else {
		modelIDs = s.cfg.Models
		if len(modelIDs) == 0 && s.cfg.Target != "" {
			modelIDs = []string{s.cfg.Target}
		}
	}
	data := make([]openAIModel, 0, len(modelIDs))
	for _, model := range modelIDs {
		model = strings.TrimSpace(model)
		if model == "" {
			continue
		}
		data = append(data, openAIModel{
			ID:      model,
			Object:  "model",
			Created: time.Now().Unix(),
			OwnedBy: "iop",
		})
	}
	writeJSON(w, http.StatusOK, openAIModelsResponse{Object: "list", Data: data})
}
