package handlers

import (
	"net/http"

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

type MeHandler struct {
	meService *service.MeService
}

func NewMeHandler(meService *service.MeService) *MeHandler {
	return &MeHandler{meService: meService}
}

func (h *MeHandler) 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
	}

	user, err := h.meService.GetMe(r.Context(), auth.UserID)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}

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