package handlers

import (
	"net/http"
	"strconv"

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

type AdminResourceHandler struct {
	service *service.AdminResourceService
}

func NewAdminResourceHandler(service *service.AdminResourceService) *AdminResourceHandler {
	return &AdminResourceHandler{service: service}
}

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

func (h *AdminResourceHandler) CreateMap(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminMapInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body: "+err.Error()))
		return
	}
	result, err := h.service.CreateMap(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminResourceHandler) CreateMapVersion(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminMapVersionInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body: "+err.Error()))
		return
	}
	result, err := h.service.CreateMapVersion(r.Context(), r.PathValue("mapPublicID"), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminResourceHandler) CreatePlayfield(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminPlayfieldInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body: "+err.Error()))
		return
	}
	result, err := h.service.CreatePlayfield(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminResourceHandler) CreatePlayfieldVersion(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminPlayfieldVersionInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body: "+err.Error()))
		return
	}
	result, err := h.service.CreatePlayfieldVersion(r.Context(), r.PathValue("playfieldPublicID"), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminResourceHandler) CreateResourcePack(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminResourcePackInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body: "+err.Error()))
		return
	}
	result, err := h.service.CreateResourcePack(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminResourceHandler) CreateResourcePackVersion(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminResourcePackVersionInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body: "+err.Error()))
		return
	}
	result, err := h.service.CreateResourcePackVersion(r.Context(), r.PathValue("resourcePackPublicID"), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

func parseAdminLimit(r *http.Request) int {
	limit := 50
	if raw := r.URL.Query().Get("limit"); raw != "" {
		if parsed, err := strconv.Atoi(raw); err == nil {
			limit = parsed
		}
	}
	return limit
}
