package handlers

import (
	"net/http"
	"strconv"

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

type AdminEventHandler struct {
	service *service.AdminEventService
}

func NewAdminEventHandler(service *service.AdminEventService) *AdminEventHandler {
	return &AdminEventHandler{service: service}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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