package org.agt.module.system.controller.admin.permission;

import cn.hutool.core.collection.CollUtil;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.framework.web.core.util.WebFrameworkUtils;
import org.agt.module.system.controller.admin.permission.vo.permission.PermissionAssignRoleDataScopeReqVO;
import org.agt.module.system.controller.admin.permission.vo.permission.PermissionAssignRoleMenuReqVO;
import org.agt.module.system.controller.admin.permission.vo.permission.PermissionAssignUserRoleReqVO;
import org.agt.module.system.service.permission.PermissionService;
import org.agt.module.system.service.tenant.TenantService;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static org.agt.framework.common.pojo.CommonResult.success;

/**
 * 权限 Controller，提供赋予用户、角色的权限的 API 接口
 *
 * @author 千通源码
 */
@Tag(name = "管理后台 - 权限")
@RestController
@RequestMapping("/system/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;
    @Resource
    private TenantService tenantService;

    @Operation(summary = "获得角色拥有的菜单编号")
    @Parameter(name = "roleId", description = "角色编号", required = true)
    @GetMapping("/list-role-menus")
    @PreAuthorize("@ss.hasPermission('system:permission:assign-role-menu')")
    public CommonResult<Set<Long>> getRoleMenuList(Long roleId) {
        Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(roleId);
        Integer[] filterMenuIds = {1138, 1139, 1140, 1141, 1142, 1143, 1224, 1225, 1226, 1227, 1228, 1229, 5010, 108, 500, 501, 1040, 1042, 1043, 1045, 107, 1036, 1037, 1038, 1039, 1093, 1094, 1095, 1096, 1097, 1098, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 2138, 2139, 2140, 2141, 2142, 2143, 2144, 2145, 2146, 2147, 2148, 2149, 2150, 2151, 2152, 2739, 109, 1046, 1048, 1261, 1263, 1264, 1265, 1266, 1267, 2447, 2448, 2449, 2450, 2451, 2452, 2453, 2083, 2, 106, 110, 114, 115, 116, 1031, 1032, 1033, 1034, 1035, 1050, 1051, 1052, 1053, 1054, 1056, 1057, 1058, 1059, 1060, 1070, 1075, 1083, 1084, 1085, 1086, 1087, 1089, 1243, 1255, 1256, 1257, 1258, 1259, 1260, 2472, 2478, 2479, 2480, 2481, 2482, 2483, 2484, 2485, 2486, 2487, 2488, 2489, 2490, 2491, 2492, 2493, 2494, 2495, 2497, 2525, 2740,1237,1238,1239,1240,1241,1242,111,1078,1082,1088,113,1066,1067,112,1077,1090,1091,1092};
        if (WebFrameworkUtils.getLoginUserId() != 2) {
            menuIds = menuIds.stream().filter(item -> Arrays.stream(filterMenuIds).noneMatch(m -> Objects.equals(Long.valueOf(m), item))
            ).collect(Collectors.toSet());
        }
        return success(menuIds);
    }

    @PostMapping("/assign-role-menu")
    @Operation(summary = "赋予角色菜单")
    @PreAuthorize("@ss.hasPermission('system:permission:assign-role-menu')")
    public CommonResult<Boolean> assignRoleMenu(@Validated @RequestBody PermissionAssignRoleMenuReqVO reqVO) {
        // 开启多租户的情况下，需要过滤掉未开通的菜单
        tenantService.handleTenantMenu(menuIds -> reqVO.getMenuIds().removeIf(menuId -> !CollUtil.contains(menuIds, menuId)));

        // 执行菜单的分配
        permissionService.assignRoleMenu(reqVO.getRoleId(), reqVO.getMenuIds());
        return success(true);
    }

    @PostMapping("/assign-role-data-scope")
    @Operation(summary = "赋予角色数据权限")
    @PreAuthorize("@ss.hasPermission('system:permission:assign-role-data-scope')")
    public CommonResult<Boolean> assignRoleDataScope(@Valid @RequestBody PermissionAssignRoleDataScopeReqVO reqVO) {
        permissionService.assignRoleDataScope(reqVO.getRoleId(), reqVO.getDataScope(), reqVO.getDataScopeDeptIds());
        return success(true);
    }

    @Operation(summary = "获得管理员拥有的角色编号列表")
    @Parameter(name = "userId", description = "用户编号", required = true)
    @GetMapping("/list-user-roles")
    @PreAuthorize("@ss.hasPermission('system:permission:assign-user-role')")
    public CommonResult<Set<Long>> listAdminRoles(@RequestParam("userId") Long userId) {
        return success(permissionService.getUserRoleIdListByUserId(userId));
    }

    @Operation(summary = "赋予用户角色")
    @PostMapping("/assign-user-role")
    @PreAuthorize("@ss.hasPermission('system:permission:assign-user-role')")
    public CommonResult<Boolean> assignUserRole(@Validated @RequestBody PermissionAssignUserRoleReqVO reqVO) {
        permissionService.assignUserRole(reqVO.getUserId(), reqVO.getRoleIds());
        return success(true);
    }

}
