package httpserver

import "net/http"

type Server struct {
	serviceName string
	ready       func() bool
	mux         *http.ServeMux
}

func New(serviceName string, ready func() bool, debug DebugAPI) *Server {
	s := &Server{serviceName: serviceName, ready: ready, mux: http.NewServeMux()}
	s.mux.HandleFunc("GET /{$}", handleRoot(serviceName))
	s.mux.HandleFunc("GET /healthz", handleHealthz(serviceName))
	s.mux.HandleFunc("GET /readyz", handleReadyz(serviceName, ready))
	s.mux.HandleFunc("GET /version", handleVersion(serviceName))

	if debug.Jobs != nil {
		s.mux.HandleFunc("GET /parse-jobs/{job_id}", handleParseJob(debug.Jobs))
		s.mux.HandleFunc("GET /diagnostics/{job_id}", handleDiagnostics(debug.Jobs))
	}
	if debug.Artifacts != nil {
		s.mux.HandleFunc("GET /artifacts/{artifact_id}", handleArtifactMetadata(debug.Artifacts))
	}

	return s
}

func (s *Server) Handler() http.Handler {
	return s.mux
}
