package jnpf.authority.controller;

import com.google.common.collect.ImmutableMap;
import jnpf.authority.service.FtbPermissionMigrateService;
import jnpf.base.ActionResult;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.exception.LoginException;
import jnpf.model.authority.dto.menu.FtbPermissionMigrareRoleAddDTO;
import jnpf.model.authority.dto.role.FtbPermissionPositionAuthorizationDTO;
import jnpf.model.authority.dto.role.FtbPermissionPositionMenuDTO;
import jnpf.model.authority.vo.menu.FtbPermissionMenuDirectoryVO;
import jnpf.model.authority.vo.menu.FtbPermissionModuleInfoVO;
import jnpf.model.authority.vo.role.FtbPermissionPositionMenuVO;
import jnpf.util.AuthUtil;
import jnpf.util.Constants;
import jnpf.util.NoDataSourceBind;
import jnpf.utils.FeignHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 权限迁移模块
 *
 * @author wangchunxiang
 * @date 2025/08/22
 */
@RestController
@RequestMapping("/permission/migrate")
public class FtbPermissionMigrateController {

    @Resource
    private FtbPermissionMigrateService ftbPermissionMigrateService;


    /**
     * 查询岗位权限菜单
     *
     * @return {@link ActionResult }<{@link List }<{@link FtbPermissionPositionMenuVO }>>
     */
    @GetMapping("/position-menu")
    @NoDataSourceBind
    public ActionResult<List<FtbPermissionPositionMenuVO>> getPositionMenuByTenantAndModule(FtbPermissionPositionMenuDTO positionMenuDTO)
            throws LoginException {
        try {
            String loginTempUserToken = AuthUtil.loginTempUser("349057407209541", positionMenuDTO.getTenantId());
            Map<String, String> headers = ImmutableMap.of(Constants.AUTHORIZATION.toLowerCase(), loginTempUserToken);
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(positionMenuDTO.getTenantId());
            List<FtbPermissionPositionMenuVO> results = ftbPermissionMigrateService.getPositionMenuByTenantAndModule(positionMenuDTO);
            return ActionResult.success(results);
        } finally {
            FeignHolder.clear();
        }
    }

    /**
     * 同步岗位权限授权
     */
    @PostMapping("/authority/sync-position-authorization")
    @NoDataSourceBind
    public ActionResult<Void> syncPositionAuthorization(@Validated @RequestBody FtbPermissionPositionAuthorizationDTO authorizationDTO)
            throws LoginException {
        TenantDataSourceUtil.switchTenant(authorizationDTO.getTenantId());
        ftbPermissionMigrateService.syncPositionAuthorization(authorizationDTO);
        return ActionResult.success();
    }
    /**
     * 根据租户id查询角色列表
     */
    @GetMapping("/get-role-list")
    @NoDataSourceBind
    public ActionResult<List<Map<String, Object>>> getRoleList(@RequestParam String tenantId) {
        return ActionResult.success(ftbPermissionMigrateService.getRoleList(tenantId));
    }

    /**
     * 新增角色
     */
    @PostMapping("/add-role")
    @NoDataSourceBind
    public ActionResult<Void> addRole(@RequestBody @Validated FtbPermissionMigrareRoleAddDTO ftbPermissionRole) {
        ftbPermissionMigrateService.addRole(ftbPermissionRole);
        return ActionResult.success();
    }
    /**
     * 查询当前角色绑定1.0权限信息
     */
    @GetMapping("/query-role-permission")
    @NoDataSourceBind
    public ActionResult<FtbPermissionModuleInfoVO> queryRolePermission(@RequestParam String roleId,
                                                                       @RequestParam String tenantId) {
        FtbPermissionModuleInfoVO ftbPermissionModuleVOList = ftbPermissionMigrateService.queryRolePermission(roleId, tenantId);
        return ActionResult.success(ftbPermissionModuleVOList);
    }

    /**
     * 根据租户Id获取菜单目录
     *
     * @param type     0岗位权限1角色权限
     * @param tenantId 租户Id
     */
    @GetMapping("/get-menu-directory")
    @NoDataSourceBind
    public ActionResult<List<FtbPermissionMenuDirectoryVO>> getMenuDirectory(@RequestParam String tenantId, @RequestParam Integer type) throws LoginException {
        try {
            String loginTempUserToken = AuthUtil.loginTempUser("349057407209541", tenantId);
            Map<String, String> headers = ImmutableMap.of(Constants.AUTHORIZATION.toLowerCase(), loginTempUserToken);
            FeignHolder.set(headers);
            TenantDataSourceUtil.switchTenant(tenantId);
            List<FtbPermissionMenuDirectoryVO> ftbPermissionMenuDirectoryVOList = ftbPermissionMigrateService.getAListOfMenuDirectory(type, tenantId);
            return ActionResult.success(ftbPermissionMenuDirectoryVOList);
        } finally {
            FeignHolder.clear();
        }
    }

}
