package api

import (
	"context"
	"net/http"
	"time"

	"github.com/alexei/docker-watcher/internal/docker"
)

// getHealth handles GET /api/health.
// Returns connectivity status for Docker with diagnostic hints on failure.
func (s *Server) getHealth(w http.ResponseWriter, r *http.Request) {
	ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
	defer cancel()

	now := time.Now().UTC().Format(time.RFC3339)

	if s.docker == nil {
		diag := docker.Diagnose(nil, "")
		respondJSON(w, http.StatusOK, map[string]any{
			"docker": map[string]any{
				"connected":  false,
				"error":      "docker client not initialized",
				"category":   diag.Category,
				"hints":      diag.Hints,
				"platform":   diag.Platform,
				"checked_at": now,
			},
		})
		return
	}

	err := s.docker.Ping(ctx)
	if err == nil {
		respondJSON(w, http.StatusOK, map[string]any{
			"docker": map[string]any{
				"connected":  true,
				"checked_at": now,
			},
		})
		return
	}

	diag := docker.Diagnose(err, "")
	respondJSON(w, http.StatusOK, map[string]any{
		"docker": map[string]any{
			"connected":  false,
			"error":      err.Error(),
			"category":   diag.Category,
			"hints":      diag.Hints,
			"platform":   diag.Platform,
			"checked_at": now,
		},
	})
}
