/*
 * Copyright 2023-2033 WanSen AI Team, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
 * with the License. A copy of the License is located at
 *
 * http://opensource.wansenai.com/apache2.0/
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */
package com.wansenai.api.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wansenai.dto.user.*;
import com.wansenai.dto.user.*;
import com.wansenai.service.user.ISysUserService;
import com.wansenai.utils.response.Response;
import com.wansenai.vo.UserInfoVO;
import com.wansenai.vo.UserListVO;
import com.wansenai.vo.UserRoleVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author James Zow
 * @since 2023-09-05
 */
@Tag(name = "用户管理", description = "用户账户相关的所有接口")
@RestController
@RequestMapping("/user")
public class SysUserController {

    private final ISysUserService userService;

    public SysUserController(ISysUserService userService) {
        this.userService = userService;
    }

    @Operation(summary = "用户注册", description = "新用户注册接口")
    @PostMapping("register")
    public Response<String> accountRegister(@RequestBody AccountRegisterDTO accountRegisterDto) {
        return userService.accountRegister(accountRegisterDto);
    }

    @Operation(summary = "用户登录", description = "用户账号密码登录")
    @PostMapping(value = "login")
    public Response<UserInfoVO> accountLogin(@RequestBody AccountLoginDTO accountLoginDto) {
        return userService.accountLogin(accountLoginDto);
    }

    @Operation(summary = "免验证码登录", description = "无需验证码的用户登录接口")
    @PostMapping(value = "loginNoCode")
    public Response<UserInfoVO> loginNoCode(@RequestBody AccountLoginDTO accountLoginDto) {
        return userService.loginNoCode(accountLoginDto);
    }

    @Operation(summary = "手机号登录", description = "使用手机号进行登录")
    @PostMapping(value = "mobileLogin")
    public Response<UserInfoVO> mobileLogin(@RequestBody MobileLoginDTO mobileLoginDto) {
        return userService.mobileLogin(mobileLoginDto);
    }

    @Operation(summary = "邮箱登录", description = "使用邮箱进行登录")
    @PostMapping(value = "emailLogin")
    public Response<UserInfoVO> emailLogin(@RequestBody EmailLoginDTO emailLoginDTO) {
        return userService.emailLogin(emailLoginDTO);
    }

    @Operation(summary = "通过手机号修改密码", description = "使用手机号验证来修改密码")
    @PostMapping(value = "updatePassword")
    public Response<String> updatePasswordByPhone(@RequestBody UpdatePasswordDto updatePasswordDto) {
        return userService.updatePassword(updatePasswordDto);
    }

    @Operation(summary = "通过邮箱修改密码", description = "使用邮箱验证来修改密码")
    @PostMapping(value = "updatePasswordByEmail")
    public Response<String> updatePasswordByEmail(@RequestBody UpdatePasswordByEmailDto updatePasswordByEmailDto) {
        return userService.updatePasswordByEmail(updatePasswordByEmailDto);
    }

    @Operation(summary = "用户修改密码", description = "已登录用户修改密码")
    @PutMapping(value = "userUpdatePassword")
    public Response<String> userUpdatePassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        return userService.resetPassword(resetPasswordDTO);
    }

    @Operation(summary = "获取操作员列表", description = "获取系统操作员列表")
    @GetMapping(value = "operator")
    public Response<List<UserInfoVO>> operator() {
        return userService.operator();
    }

    @Operation(summary = "获取用户信息", description = "获取当前登录用户的详细信息")
    @GetMapping(value = "info")
    public Response<UserInfoVO> info() {
        return userService.userInfo();
    }

    @Operation(summary = "获取用户权限", description = "获取当前用户的角色权限信息")
    @GetMapping(value = "perm")
    public Response<List<UserRoleVO>> permission() {
        return userService.userRole();
    }

    @Operation(summary = "用户登出", description = "用户退出登录")
    @GetMapping(value = "logout")
    public Response<String> logout() {
        return userService.userLogout();
    }

    @Operation(summary = "分页获取用户列表", description = "分页查询用户信息列表")
    @PostMapping(value = "list")
    public Response<Page<UserListVO>> list(@RequestBody UserListDTO userListDto) {
        return userService.userList(userListDto);
    }

    @Operation(summary = "获取所有用户", description = "获取所有用户信息列表")
    @GetMapping(value = "listAll")
    public Response<List<UserListVO>> listAll() {
        return userService.userListAll();
    }

    @Operation(summary = "更新用户信息", description = "更新用户基本信息")
    @PostMapping(value = "update")
    public Response<String> update(@RequestBody UpdateUserDTO updateUserDTO) {
        return userService.updateUser(updateUserDTO);
    }

    @Operation(summary = "更新用户状态", description = "更新用户启用/禁用状态")
    @PostMapping(value = "updateStatus")
    public Response<String> updateStatus(@RequestBody UpdateUserDTO updateUserDTO) {
        return userService.updateStatus(updateUserDTO);
    }

    @Operation(summary = "上传用户头像", description = "上传并更新用户头像")
    @PostMapping("uploadAvatar")
    public Response<String> uploadAvatar(
            @Parameter(description = "头像文件") @RequestParam("file") MultipartFile file,
            @Parameter(description = "用户ID") @RequestParam("userId") Long userId,
            @Parameter(description = "文件名") @RequestParam("name") String name) {
        return userService.uploadAvatar(file, userId, name);
    }

    @Operation(summary = "新增或更新用户", description = "新增用户或更新用户信息")
    @PostMapping(value = "addOrUpdate")
    public Response<String> addOrUpdate(@RequestBody AddOrUpdateUserDTO addOrUpdateUserDTO) {
        return userService.addOrUpdate(addOrUpdateUserDTO);
    }

    @Operation(summary = "删除用户", description = "批量删除用户")
    @PostMapping(value = "delete")
    public Response<String> deleteUser(@Parameter(description = "用户ID列表") @RequestParam(value = "ids") List<Long> ids) {
        return userService.deleteUser(ids);
    }

    @Operation(summary = "重置用户密码", description = "将用户密码重置为默认密码")
    @PostMapping(value = "resetPassword")
    public Response<String> resetPassword(@Parameter(description = "用户ID") @RequestParam(value = "id") Long id) {
        return userService.resetPassword(id);
    }

    @Operation(summary = "重置手机号", description = "更新用户手机号")
    @PutMapping(value = "resetPhoneNumber")
    public Response<String> resetPhoneNumber(@RequestBody ResetPhoneDTO resetPhoneDTO) {
        return userService.resetPhoneNumber(resetPhoneDTO);
    }

    @Operation(summary = "重置邮箱", description = "更新用户邮箱")
    @PutMapping(value = "resetEmail")
    public Response<String> resetEmail(@RequestBody ResetEmailDTO resetEmailDTO) {
        return userService.resetEmail(resetEmailDTO);
    }
}
