package com.agileboot.system.user.controller;

import cn.hutool.core.collection.ListUtil;
import com.agileboot.common.core.core.R;
import com.agileboot.common.core.utils.poi.CustomExcelUtil;
import com.agileboot.common.mybatis.core.page.PageR;
import com.agileboot.system.client.entity.SysClient;
import com.agileboot.system.client.service.ISysClientService;
import com.agileboot.system.client.vo.SysClientVO;
import com.agileboot.system.user.dto.*;
import com.agileboot.system.user.service.ISysUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
 * 用户信息
 */
@RestController
@RequestMapping("/system/users")
@RequiredArgsConstructor
public class SysUserController {

    private final ISysUserService sysUserService;
    private final ISysClientService sysClientService;

    /**
     * 获取用户列表
     */
    @GetMapping("list")
    public PageR<UserInfo> userList(SearchUserQuery dto) {
        IPage<UserInfo> page = sysUserService.getUserList(dto);
        return new PageR<>(page);
    }

    /**
     * 用户列表导出
     */
    @GetMapping("/excel")
    public void exportUserByExcel(HttpServletResponse response, SearchUserQuery dto) {
        IPage<UserInfo> page = sysUserService.getUserList(dto);
        CustomExcelUtil.writeToResponse(page.getRecords(), UserInfo.class, response);
    }

    /**
     * 用户列表导入
     */
    @PostMapping("/excel")
    public R<Void> importUserByExcel(MultipartFile file) {
        List<AddUserCommand> commands = CustomExcelUtil.readFromRequest(AddUserCommand.class, file);

        for (AddUserCommand command : commands) {
            sysUserService.addUser(command);
        }
        return R.ok();
    }

    /**
     * 下载批量导入模板
     */
    @GetMapping("/excelTemplate")
    public void downloadExcelTemplate(HttpServletResponse response) {
        CustomExcelUtil.writeToResponse(ListUtil.toList(new AddUserCommand()), AddUserCommand.class, response);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping("/{userId}")
    public R<UserDetailDTO> getUserDetailInfo(@PathVariable(value = "userId", required = false) Long userId) {
        UserDetailDTO userDetailInfo = sysUserService.getUserDetailInfo(userId);
        return R.ok(userDetailInfo);
    }

    /**
     * 获取所有客户端
     */
    @GetMapping("/client")
    public R<List<SysClientVO>> getClient() {
        List<SysClient> list = sysClientService.list();
        List<SysClientVO> clientVOS = list.stream().map(SysClientVO::new).toList();
        return R.ok(clientVOS);
    }

    /**
     * 新增用户
     */
    @PostMapping("/create")
    public R<Void> add(@Validated @RequestBody AddUserCommand command) {
        sysUserService.addUser(command);
        return R.ok();
    }

    /**
     * 修改用户
     */
    @PostMapping("/update/{userId}")
    public R<Void> edit(@PathVariable(value = "userId") Long userId, @Validated @RequestBody UpdateUserCommand command) {
        command.setUserId(userId);
        sysUserService.updateUser(command);
        return R.ok();
    }

    /**
     * 删除用户
     */
    @PostMapping("/del/{userId}")
    public R<Void> remove(@PathVariable(value = "userId") Long userId) {
        sysUserService.deleteUsers(Set.of(userId));
        return R.ok();
    }

    /**
     * 管理员重置用户密码
     */
    @PostMapping("/re_pwd_ad/{userId}")
    public R<Void> resetPasswordByAdmin(@PathVariable(value = "userId") Long userId, @RequestBody ResetPasswordCommand command) {
        command.setUserId(userId);
        sysUserService.resetUserPassword(command);
        return R.ok();
    }

    /**
     * 重置密码
     */
    @PostMapping("/re_pwd")
    public R<Void> resetPassword(@RequestBody ResetPasswordCommand command) {
        sysUserService.resetUserPasswordByAdmin(command);
        return R.ok();
    }

    /**
     * 修改用户状态
     */
    @PostMapping("/status/{userId}")
    public R<Void> changeStatus(@PathVariable(value = "userId") Long userId, @RequestBody ChangeStatusCommand command) {
        command.setUserId(userId);
        sysUserService.changeUserStatus(command);
        return R.ok();
    }

}
