package appconfig

import (
	"fmt"

	"hyapp-admin-server/internal/appctx"
	"hyapp-admin-server/internal/modules/shared"
	"hyapp-admin-server/internal/response"

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

func (h *Handler) GetRoomDiscoveryConfig(c *gin.Context) {
	item, err := h.service.GetRoomDiscoveryConfig(appctx.FromContext(c.Request.Context()))
	if err != nil {
		response.ServerError(c, "获取房间发现范围失败")
		return
	}
	response.OK(c, item)
}

func (h *Handler) UpdateRoomDiscoveryConfig(c *gin.Context) {
	var request roomDiscoveryConfigRequest
	if err := c.ShouldBindJSON(&request); err != nil {
		response.BadRequest(c, "参数不正确")
		return
	}
	appCode := appctx.FromContext(c.Request.Context())
	item, err := h.service.UpdateRoomDiscoveryConfig(appCode, request)
	if err != nil {
		response.BadRequest(c, err.Error())
		return
	}
	// 运行时 gateway 最多在 room_discovery_cache_ttl 后读取新值；审计保留 App 和最终规范化范围，便于定位缓存窗口内差异。
	shared.OperationLog(c, h.audit, "update-room-discovery-scope", "admin_app_configs", "success", fmt.Sprintf("app_code=%s scope=%s", item.AppCode, item.Scope))
	response.OK(c, item)
}
