package com.njcn.rdms.module.system.api.permission;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.util.collection.CollectionUtils;
import com.njcn.rdms.module.system.api.permission.dto.ObjectRolePermissionRespDTO;
import com.njcn.rdms.module.system.api.permission.dto.ObjectRoleRespDTO;
import com.njcn.rdms.module.system.enums.ApiConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@FeignClient(name = ApiConstants.NAME)
@Tag(name = "RPC 服务 - 对象权限")
public interface ObjectPermissionApi {

    String PREFIX = ApiConstants.PREFIX + "/permission/object";

    @GetMapping(PREFIX + "/role-by-id")
    @Operation(summary = "按角色 ID 查询对象作用域角色摘要")
    @Parameter(name = "roleId", description = "角色 ID", example = "1024", required = true)
    @Parameter(name = "scopeType", description = "权限作用域类型", example = "object", required = true)
    @Parameter(name = "objectType", description = "对象类型", example = "product", required = true)
    CommonResult<ObjectRoleRespDTO> getObjectRoleById(@RequestParam("roleId") Long roleId,
                                                      @RequestParam("scopeType") String scopeType,
                                                      @RequestParam("objectType") String objectType);

    @GetMapping(PREFIX + "/role-by-code")
    @Operation(summary = "按角色编码查询对象作用域角色摘要")
    @Parameter(name = "roleCode", description = "角色编码", example = "product_manager", required = true)
    @Parameter(name = "scopeType", description = "权限作用域类型", example = "object", required = true)
    @Parameter(name = "objectType", description = "对象类型", example = "product", required = true)
    CommonResult<ObjectRoleRespDTO> getObjectRoleByCode(@RequestParam("roleCode") String roleCode,
                                                        @RequestParam("scopeType") String scopeType,
                                                        @RequestParam("objectType") String objectType);

    @GetMapping(PREFIX + "/role-list")
    @Operation(summary = "按角色 ID 批量查询对象作用域角色摘要")
    @Parameter(name = "roleIds", description = "角色 ID 集合", example = "1,2", required = true)
    @Parameter(name = "scopeType", description = "权限作用域类型", example = "object", required = true)
    @Parameter(name = "objectType", description = "对象类型", example = "product", required = true)
    CommonResult<List<ObjectRoleRespDTO>> getObjectRoleList(@RequestParam("roleIds") Collection<Long> roleIds,
                                                            @RequestParam("scopeType") String scopeType,
                                                            @RequestParam("objectType") String objectType);

    @GetMapping(PREFIX + "/permissions")
    @Operation(summary = "查询对象作用域角色权限集合")
    @Parameter(name = "roleId", description = "角色 ID", example = "1024", required = true)
    @Parameter(name = "scopeType", description = "权限作用域类型", example = "object", required = true)
    @Parameter(name = "objectType", description = "对象类型", example = "product", required = true)
    CommonResult<Set<String>> getObjectRolePermissions(@RequestParam("roleId") Long roleId,
                                                       @RequestParam("scopeType") String scopeType,
                                                       @RequestParam("objectType") String objectType);

    @GetMapping(PREFIX + "/role-permission-detail")
    @Operation(summary = "查询对象作用域角色菜单与权限聚合结果")
    @Parameter(name = "roleId", description = "角色 ID", example = "1024", required = true)
    @Parameter(name = "scopeType", description = "权限作用域类型", example = "object", required = true)
    @Parameter(name = "objectType", description = "对象类型", example = "product", required = true)
    CommonResult<ObjectRolePermissionRespDTO> getObjectRolePermissionDetail(@RequestParam("roleId") Long roleId,
                                                                            @RequestParam("scopeType") String scopeType,
                                                                            @RequestParam("objectType") String objectType);

    @GetMapping(PREFIX + "/role-permission-detail-merged")
    @Operation(summary = "按 roleId 列表聚合菜单 + 权限码（多角色场景）；主角色按 system_role.sort 升序取首个，菜单/权限取并集")
    @Parameter(name = "roleIds", description = "角色 ID 集合", example = "1,2", required = true)
    @Parameter(name = "scopeType", description = "权限作用域类型", example = "object", required = true)
    @Parameter(name = "objectType", description = "对象类型", example = "product", required = true)
    CommonResult<ObjectRolePermissionRespDTO> getObjectRolePermissionDetailMerged(@RequestParam("roleIds") Collection<Long> roleIds,
                                                                                  @RequestParam("scopeType") String scopeType,
                                                                                  @RequestParam("objectType") String objectType);

    /**
     * 按角色 ID 返回对象作用域角色摘要映射，便于业务模块批量对齐本地成员数据。
     *
     * @param roleIds 角色 ID 集合
     * @param scopeType 权限作用域类型
     * @param objectType 对象类型
     * @return 角色摘要映射
     */
    default Map<Long, ObjectRoleRespDTO> getObjectRoleMap(Collection<Long> roleIds, String scopeType, String objectType) {
        if (CollUtil.isEmpty(roleIds)) {
            return MapUtil.empty();
        }
        List<ObjectRoleRespDTO> roles = getObjectRoleList(roleIds, scopeType, objectType).getCheckedData();
        return CollectionUtils.convertMap(roles, ObjectRoleRespDTO::getId);
    }

}
