package handlers

import (
	"net/http"

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

type AdminProductionHandler struct {
	service *service.AdminProductionService
}

func NewAdminProductionHandler(service *service.AdminProductionService) *AdminProductionHandler {
	return &AdminProductionHandler{service: service}
}

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

func (h *AdminProductionHandler) CreatePlace(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminPlaceInput
	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.CreatePlace(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminProductionHandler) CreateMapAsset(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminMapAssetInput
	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.CreateMapAsset(r.Context(), r.PathValue("placePublicID"), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminProductionHandler) CreateTileRelease(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminTileReleaseInput
	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.CreateTileRelease(r.Context(), r.PathValue("mapAssetPublicID"), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminProductionHandler) CreateCourseSource(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminCourseSourceInput
	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.CreateCourseSource(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminProductionHandler) CreateCourseSet(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminCourseSetInput
	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.CreateCourseSet(r.Context(), r.PathValue("mapAssetPublicID"), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminProductionHandler) CreateCourseVariant(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminCourseVariantInput
	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.CreateCourseVariant(r.Context(), r.PathValue("courseSetPublicID"), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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

func (h *AdminProductionHandler) CreateRuntimeBinding(w http.ResponseWriter, r *http.Request) {
	var req service.CreateAdminRuntimeBindingInput
	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.CreateRuntimeBinding(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusCreated, map[string]any{"data": result})
}

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