package server

import (
	"net/http"

	"galaxy/backend/internal/server/handlers"
	"galaxy/backend/internal/user"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// InternalUsersHandlers groups the gateway-only user-fetch handlers
// under `/api/v1/internal/users/*`. The current implementation ships real
// implementations backed by `*user.Service`; tests that supply a nil
// service fall back to the Stage-3 placeholder body.
type InternalUsersHandlers struct {
	svc *user.Service
	logger *zap.Logger
}

// NewInternalUsersHandlers constructs the handler set. svc may be
// nil — in that case every handler returns 501 not_implemented.
// logger may also be nil; zap.NewNop is used in that case.
func NewInternalUsersHandlers(svc *user.Service, logger *zap.Logger) *InternalUsersHandlers {
	if logger == nil {
		logger = zap.NewNop()
	}
	return &InternalUsersHandlers{svc: svc, logger: logger.Named("http.internal.users")}
}

// GetAccountInternal handles GET /api/v1/internal/users/{user_id}/account-internal.
func (h *InternalUsersHandlers) GetAccountInternal() gin.HandlerFunc {
	if h.svc == nil {
		return handlers.NotImplemented("internalUsersGetAccountInternal")
	}
	return func(c *gin.Context) {
		userID, ok := parseUserIDParam(c)
		if !ok {
			return
		}
		ctx := c.Request.Context()
		account, err := h.svc.GetAccount(ctx, userID)
		if err != nil {
			respondAccountError(c, h.logger, "internal users get account", ctx, err)
			return
		}
		c.JSON(http.StatusOK, accountResponseToWire(account))
	}
}
