package handlers

import (
	"net/http"
	"strconv"

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

type ConfigHandler struct {
	configService *service.ConfigService
}

func NewConfigHandler(configService *service.ConfigService) *ConfigHandler {
	return &ConfigHandler{configService: configService}
}

func (h *ConfigHandler) ListLocalFiles(w http.ResponseWriter, r *http.Request) {
	result, err := h.configService.ListLocalEventFiles()
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *ConfigHandler) ImportLocal(w http.ResponseWriter, r *http.Request) {
	var req service.ImportLocalEventConfigInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
		return
	}
	req.EventPublicID = r.PathValue("eventPublicID")

	result, err := h.configService.ImportLocalEventConfig(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *ConfigHandler) BuildPreview(w http.ResponseWriter, r *http.Request) {
	var req service.BuildPreviewInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
		return
	}

	result, err := h.configService.BuildPreview(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *ConfigHandler) PublishBuild(w http.ResponseWriter, r *http.Request) {
	var req service.PublishBuildInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
		return
	}

	result, err := h.configService.PublishBuild(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *ConfigHandler) ListSources(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.configService.ListEventConfigSources(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 *ConfigHandler) GetSource(w http.ResponseWriter, r *http.Request) {
	result, err := h.configService.GetEventConfigSource(r.Context(), r.PathValue("sourceID"))
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *ConfigHandler) GetBuild(w http.ResponseWriter, r *http.Request) {
	result, err := h.configService.GetEventConfigBuild(r.Context(), r.PathValue("buildID"))
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}
