package cn.reghao.devops.mgr.admin.controller;

import cn.reghao.devops.mgr.admin.model.dto.UpdatePasswordDto;
import cn.reghao.devops.mgr.admin.model.dto.CreateAccountDto;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.devops.mgr.admin.model.dto.AccountProfile;
import cn.reghao.devops.mgr.admin.model.dto.AccountRole;
import cn.reghao.devops.mgr.admin.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Api(tags = "用户接口")
@RequestMapping("/api/rbac/user")
@RestController
public class UserController {
    private final AccountService accountService;

    public UserController(AccountService accountService) {
        this.accountService = accountService;
    }

    @ApiOperation(value = "修改个人头像", notes = "N")
    @PostMapping(value = "/avatar", produces = MediaType.APPLICATION_JSON_VALUE)
    public String userAvatar(MultipartFile file) {
        return WebResult.failWithMsg("接口未实现");
    }

    @ApiOperation(value = "修改个人信息", notes = "N")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public String editUserInfo(@Validated AccountProfile accountProfile) {
        accountService.updateAccountProfile(accountProfile);
        return WebResult.success();
    }

    @ApiOperation(value = "修改个人密码", notes = "N")
    @PostMapping(value = "/passwd/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String editPasswd(UpdatePasswordDto updatePasswordDto) {
        return WebResult.failWithMsg("接口未实现");
    }

    @ApiOperation(value = "创建用户", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public String createUser(@Validated CreateAccountDto createAccountDto) {
        Result result = accountService.createAccount(createAccountDto);
        return WebResult.result(result);
    }

    @ApiOperation(value = "批量创建用户", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/batch", produces = MediaType.APPLICATION_JSON_VALUE)
    public String batchAdd(MultipartFile file) {
        return WebResult.failWithMsg("接口未实现");
    }

    @ApiOperation(value = "删除用户", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @DeleteMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteAccount(@PathVariable("id") Integer userId) {
        Result result = accountService.deleteAccount(userId);
        return WebResult.result(result);
    }

    @ApiOperation(value = "修改用户密码", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/passwd", produces = MediaType.APPLICATION_JSON_VALUE)
    public String modifyPassword(@NotNull Integer id, @NotNull String newPassword) {
        accountService.updateAccountPassword(id, newPassword);
        return WebResult.success();
    }

    @ApiOperation(value = "分配用户角色", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/role", produces = MediaType.APPLICATION_JSON_VALUE)
    public String assignRole(@Validated AccountRole accountRole) {
        Result result = accountService.updateAccountRole(accountRole);
        return WebResult.result(result);
    }

    @ApiOperation(value = "启用/禁用用户", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/status/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateAccountStatus(@PathVariable("userId") Integer userId) {
        //userIds.forEach(userId -> accountService.updateAccountStatus(userId, enable));
        return WebResult.failWithMsg("接口未实现");
    }
}
