package handlers

import (
	"net/http"
	"strconv"

	"cmr-backend/internal/httpx"
	"cmr-backend/internal/service"
)

type MapExperienceHandler struct {
	service *service.MapExperienceService
}

func NewMapExperienceHandler(service *service.MapExperienceService) *MapExperienceHandler {
	return &MapExperienceHandler{service: service}
}

func (h *MapExperienceHandler) ListMaps(w http.ResponseWriter, r *http.Request) {
	limit := 20
	if raw := r.URL.Query().Get("limit"); raw != "" {
		if parsed, err := strconv.Atoi(raw); err == nil {
			limit = parsed
		}
	}
	result, err := h.service.ListMaps(r.Context(), service.ListExperienceMapsInput{Limit: limit})
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *MapExperienceHandler) GetMapDetail(w http.ResponseWriter, r *http.Request) {
	result, err := h.service.GetMapDetail(r.Context(), r.PathValue("mapAssetPublicID"))
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}
