// Package admin provides HTTP handlers for admin endpoints.
package admin

import (
	"encoding/json"
	"net/http"

	"github.com/tidbcloud/cluster-service-ng/hack/mock-dpm/store"
)

// Handler handles admin HTTP requests.
type Handler struct {
	store *store.MemoryStore
}

// NewHandler creates a new admin handler.
func NewHandler(s *store.MemoryStore) *Handler {
	return &Handler{store: s}
}

// Healthz handles health check requests.
func (h *Handler) Healthz(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}

// Dump returns all stored data for debugging.
func (h *Handler) Dump(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	data := h.store.Dump()
	json.NewEncoder(w).Encode(data)
}

// Reset clears all stored data.
func (h *Handler) Reset(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	h.store.Reset()
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]string{"status": "reset successful"})
}
