package handlers

import (
	"net/http"

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

type EventPlayHandler struct {
	eventPlayService *service.EventPlayService
}

func NewEventPlayHandler(eventPlayService *service.EventPlayService) *EventPlayHandler {
	return &EventPlayHandler{eventPlayService: eventPlayService}
}

func (h *EventPlayHandler) Get(w http.ResponseWriter, r *http.Request) {
	auth := middleware.GetAuthContext(r.Context())
	if auth == nil {
		httpx.WriteError(w, apperr.New(http.StatusUnauthorized, "unauthorized", "missing auth context"))
		return
	}

	result, err := h.eventPlayService.GetEventPlay(r.Context(), service.EventPlayInput{
		EventPublicID: r.PathValue("eventPublicID"),
		UserID:        auth.UserID,
	})
	if err != nil {
		httpx.WriteError(w, err)
		return
	}

	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}
