package com.yinlihupo.enlish.service.controller;

import com.yinlihupo.enlish.service.constant.UserRedisConstants;
import com.yinlihupo.enlish.service.domain.dataobject.RoleDO;
import com.yinlihupo.enlish.service.domain.dataobject.UserDO;
import com.yinlihupo.enlish.service.model.vo.admin.CreateInvitationCodeReqVO;
import com.yinlihupo.enlish.service.model.vo.admin.CreateInvitationCodeRspVO;
import com.yinlihupo.enlish.service.model.vo.user.CreateUserReqVO;
import com.yinlihupo.enlish.service.model.vo.user.FindUserListRepVO;
import com.yinlihupo.enlish.service.model.vo.user.FindUserListRspVO;
import com.yinlihupo.enlish.service.service.RoleService;
import com.yinlihupo.enlish.service.service.UserService;
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
import com.yinlihupo.framework.common.response.PageResponse;
import com.yinlihupo.framework.common.response.Response;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RequestMapping("/admin/")
@RestController
public class AdminController {

    @Resource
    private UserService userService;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private RoleService roleService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("user/list")
    @ApiOperationLog(description = "查询用户列表")
    public PageResponse<FindUserListRspVO> listUser(@RequestBody FindUserListRepVO findUserListRepVO) {
        Integer userTotal = userService.findUserTotal();
        if (userTotal == null || userTotal == 0) {
            throw new RuntimeException("暂无用户");
        }

        Integer page = findUserListRepVO.getPage();
        Integer pageSize = findUserListRepVO.getPageSize();
        String name = findUserListRepVO.getName();
        List<UserDO> usersList = userService.findUsersList(page, pageSize, name.isEmpty() ? null : name);
        Map<Long, RoleDO> userId2RoleMap = roleService.findUserId2RoleMap(usersList.stream().map(UserDO::getId).toList());
        return PageResponse.success(usersList.stream().map(userDO -> FindUserListRspVO.builder()
                .id(userDO.getId())
                .name(userDO.getName())
                .phone(userDO.getPhone())
                .roleName(userId2RoleMap.get(userDO.getId()).getRoleName())
                .build()).toList(), page, userTotal, pageSize);
    }

    @PostMapping("user/create")
    public Response<Void> createUser(@RequestBody CreateUserReqVO createUserReqVO) {
        String name = createUserReqVO.getName();
        String phone = createUserReqVO.getPhone();
        String password = createUserReqVO.getPassword();

        userService.createUser(UserDO.builder()
                .name(name)
                .phone(phone)
                .password(passwordEncoder.encode(password))
                .build());

        return Response.success();
    }

    @PostMapping("user/create/invitation/code")
    @ApiOperationLog(description = "创建邀请码")
    public Response<CreateInvitationCodeRspVO> createInvitationCode(@RequestBody CreateInvitationCodeReqVO createInvitationCodeReqVO) {

        String code = String.valueOf(new Random().nextInt(1000000));
        Integer limit = createInvitationCodeReqVO.getLimit();
        Integer expire = createInvitationCodeReqVO.getExpire();

        redisTemplate.opsForValue().set(UserRedisConstants.buildUserInvitationCode(code), limit);
        redisTemplate.expire(UserRedisConstants.buildUserInvitationCode(code), expire, TimeUnit.DAYS);

        return Response.success(CreateInvitationCodeRspVO.builder().invitationCode(code).build());
    }
}
