/**
 * 
 */
package org.springblade.modules.auth.endpoint;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springblade.core.constant.RoleConstant;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.exception.ServiceException;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.modules.auth.granter.UserIdTokenGranter;
import org.springblade.modules.auth.provider.ITokenGranter;
import org.springblade.modules.auth.provider.TokenGranterBuilder;
import org.springblade.modules.auth.provider.TokenParameter;
import org.springblade.modules.auth.utils.TokenUtil;
import org.springblade.modules.system.entity.Role;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserInfo;
import org.springblade.modules.system.service.IRoleService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiSort;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

/**
 * @author wookvn
 *
 */
@ApiSort(1)
@RestController
@AllArgsConstructor
@RequestMapping("/platomix-auth")
@Api(value = "用户模拟登录认证", tags = "用户模拟登录认证")
public class BladeMockLoginEndPoint {
	
	private final IRoleService roleService;
	private final IUserService userService;

	@GetMapping("/mock-login/{tenantId}")
	@ApiOperation(value = "模拟登录")
	public Kv mockLogin(@PathVariable String tenantId) {
		if(!AuthUtil.isAdministrator()) {
			throw new ServiceException("只有运营人员拥有模拟登录权限");
		}
		List<Role> roleList = roleService.list(Wrappers.<Role>lambdaQuery().eq(Role::getRoleAlias, RoleConstant.ADMIN).eq(Role::getTenantId, tenantId));
		if(CollectionUtil.isEmpty(roleList)) {
			throw new ServiceException("该租户没有管理员角色");
		}
		List<User> userList = userService.list(Wrappers.<User>lambdaQuery().eq(User::getTenantId, tenantId).like(User::getRoleId, roleList.get(0).getId().toString()));
		if(CollectionUtil.isEmpty(roleList)) {
			throw new ServiceException("该租户没有管理员角色的用户");
		}
		Kv authInfo = Kv.create();
		TokenParameter tokenParameter = new TokenParameter();
		tokenParameter.getArgs().set("userId", userList.get(0).getId().toString());

		ITokenGranter granter = TokenGranterBuilder.getGranter(UserIdTokenGranter.GRANT_TYPE);
		UserInfo userInfo = granter.grant(tokenParameter);

		if (userInfo == null || userInfo.getUser() == null) {
			return authInfo.set("error_code", HttpServletResponse.SC_BAD_REQUEST).set("error_description", "用户名或密码不正确");
		}
//		if (userInfo.getUser().getIsLogin() == 1) {
//			return authInfo.set("error_code", HttpServletResponse.SC_BAD_REQUEST).set("error_description", "该用户不能使用账号密码登录");
//		}
		return TokenUtil.createAuthInfo(userInfo, 1);
	}
}
