package roomrocket

import (
	"errors"

	"hyapp-admin-server/internal/integration/roomclient"
	"hyapp-admin-server/internal/middleware"
	"hyapp-admin-server/internal/modules/shared"
	"hyapp-admin-server/internal/response"

	"github.com/gin-gonic/gin"
)

type Handler struct {
	service *Service
	audit   shared.OperationLogger
}

func New(roomClient roomclient.Client, audit shared.OperationLogger) *Handler {
	return &Handler{service: NewService(roomClient), audit: audit}
}

func (h *Handler) GetRoomRocketConfig(c *gin.Context) {
	config, err := h.service.GetConfig(c.Request.Context())
	if err != nil {
		response.ServerError(c, "获取房间火箭配置失败")
		return
	}
	response.OK(c, config)
}

func (h *Handler) UpdateRoomRocketConfig(c *gin.Context) {
	var req updateRoomRocketConfigRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "房间火箭配置参数不正确")
		return
	}
	config, err := h.service.UpdateConfig(c.Request.Context(), req, int64(middleware.CurrentUserID(c)))
	if err != nil {
		if errors.Is(err, ErrInvalidArgument) {
			response.BadRequest(c, err.Error())
			return
		}
		response.ServerError(c, "更新房间火箭配置失败")
		return
	}
	shared.OperationLogWithResourceID(c, h.audit, "update-room-rocket-config", "room_rocket_configs", config.AppCode, "success", "")
	response.OK(c, config)
}
