package appconfig

import (
	"fmt"

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

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

func (h *Handler) GetRoleScopePoliciesConfig(c *gin.Context) {
	item, err := h.service.GetRoleScopePoliciesConfig(c.Request.Context(), appctx.FromContext(c.Request.Context()), middleware.CurrentRequestID(c))
	if err != nil {
		response.ServerError(c, "获取角色范围配置失败")
		return
	}
	response.OK(c, item)
}

func (h *Handler) UpdateRoleScopePoliciesConfig(c *gin.Context) {
	var request roleScopePoliciesConfigRequest
	if err := c.ShouldBindJSON(&request); err != nil {
		response.BadRequest(c, "参数不正确")
		return
	}
	item, err := h.service.UpdateRoleScopePoliciesConfig(c.Request.Context(), appctx.FromContext(c.Request.Context()), middleware.CurrentRequestID(c), request)
	if err != nil {
		response.BadRequest(c, err.Error())
		return
	}
	// 审计只记录规范化范围枚举；真正配置由 user-service 写 user DB，后台不保存第二份可漂移副本。
	shared.OperationLog(c, h.audit, "update-role-scope-policies", "role_scope_policies", "success", fmt.Sprintf(
		"app_code=%s organization_scope=%s manager_base_scope=%s manager_expanded_scope=%s manager_region_expansion_configurable=%t",
		item.AppCode, item.OrganizationScope, item.ManagerBaseScope, item.ManagerExpandedScope, item.ManagerRegionExpansionConfigurable,
	))
	response.OK(c, item)
}
