package appapi

import (
	"net/http"

	userv1 "hyapp.local/api/proto/user/v1"
	"hyapp/services/gateway-service/internal/auth"
	"hyapp/services/gateway-service/internal/transport/http/httpkit"
)

type appHeartbeatData struct {
	Accepted      bool  `json:"accepted"`
	HeartbeatAtMS int64 `json:"heartbeat_at_ms"`
	ServerTimeMS  int64 `json:"server_time_ms"`
}

// appHeartbeat 刷新当前登录 App 会话，不写房间 presence，也不改变麦位在线状态。
func (h *Handler) appHeartbeat(writer http.ResponseWriter, request *http.Request) {
	if h.userAuthClient == nil {
		// App 心跳必须落 user-service session；未注入时不能返回假在线。
		httpkit.WriteError(writer, request, http.StatusBadGateway, httpkit.CodeUpstreamError, "upstream service error")
		return
	}

	sessionID := auth.SessionIDFromContext(request.Context())
	resp, err := h.userAuthClient.AppHeartbeat(request.Context(), &userv1.AppHeartbeatRequest{
		Meta:      httpkit.UserMeta(request, httpkit.FirstHeader(request, "X-Device-ID", "X-HY-Device-ID")),
		UserId:    auth.UserIDFromContext(request.Context()),
		SessionId: sessionID,
	})
	if err != nil {
		httpkit.WriteRPCError(writer, request, err)
		return
	}

	httpkit.WriteOK(writer, request, appHeartbeatData{
		Accepted:      resp.GetAccepted(),
		HeartbeatAtMS: resp.GetHeartbeatAtMs(),
		ServerTimeMS:  resp.GetServerTimeMs(),
	})
}
