package cn.meowrain.aioj.backend.userservice.controller;

import cn.meowrain.aioj.backend.framework.core.web.Result;
import cn.meowrain.aioj.backend.framework.core.web.Results;
import cn.meowrain.aioj.backend.userservice.dto.req.*;
import cn.meowrain.aioj.backend.userservice.dto.resp.UserAuthRespDTO;
import cn.meowrain.aioj.backend.userservice.dto.resp.UserProfileRespDTO;
import cn.meowrain.aioj.backend.userservice.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController()
@RequestMapping("/v1/user")
@Tag(name = "用户管理", description = "用户注册、查询等接口")
public class UserController {

    private final UserService userService;


    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "根据用户注册信息创建新用户")
    public Result<Long> register(
            @Parameter(description = "用户注册信息", required = true)
            @RequestBody UserRegisterRequestDTO userRegisterRequest) {
        Long l = userService.userRegister(userRegisterRequest);
        return Results.success(l);
    }

    @GetMapping("/inner/get-by-username")
    @Operation(summary = "根据用户名查询用户", description = "通过用户账号获取用户认证信息(内部接口)")
    public Result<UserAuthRespDTO> getUserByUserName(
            @Parameter(description = "用户账号", required = true, example = "admin")
            @RequestParam("userAccount") String userAccount) {
        UserAuthRespDTO userAuthDTO = userService.findAuthInfoByUserAccount(userAccount);
        return Results.success(userAuthDTO);
    }

    @GetMapping("/inner/get-by-userid")
    @Operation(summary = "根据用户ID查询用户", description = "通过用户ID获取用户认证信息(内部接口)")
    public Result<UserAuthRespDTO> getUserById(
            @Parameter(description = "用户ID", required = true, example = "1")
            @RequestParam("userId") Long userid) {
        UserAuthRespDTO userAuthRespDTO = userService.findAuthInfoByUserId(userid);
        return Results.success(userAuthRespDTO);
    }
    /*			================================邮箱相关接口 ======================*/

    /**
     * 发送验证码
     */
    @Operation(summary = "发送验证码", description = "根据用户注册的邮箱发送验证码")
    @GetMapping("/email/send-code")
    public Result<Void> getVerifyCode(@Parameter(description = "邮箱信息", required = true)
                                      @Valid @ModelAttribute EmailSendCodeRequestDTO request) {
        userService.sendEmailCode(request.getEmail());
        return Results.success(null);
    }

    /**
     * 绑定邮箱
     *
     * @param request 邮箱绑定请求体
     * @return
     */
    @Operation(summary = "绑定邮箱", description = "根据用户注册的邮箱绑定邮箱")
    @PostMapping("/email/bind")
    public Result<Void> bindEmail(@RequestBody @Valid BindEmailRequest request) {
        userService.bindEmail(request.getEmail(), request.getVerifyCode());
        return Results.success(null);
    }

    /**
     * 解绑邮箱
     *
     * @return
     */
    @Operation(summary = "解绑邮箱", description = "根据用户注册的邮箱解绑邮箱")
    @PostMapping("/email/unbind")
    public Result<Void> unbindEmail() {
        userService.unbindEmail();
        return Results.success(null);
    }


    @Operation(summary = "个人资料管理", description = "获取完整个人资料")
    @GetMapping("/profile")
    public Result<UserProfileRespDTO> getUserProfile() {
        UserProfileRespDTO userProfileRespDTO = userService.getUserProfile();
        return Results.success(userProfileRespDTO);
    }

    @Operation(summary = "个人资料管理-更新个人资料", description = "更新个人资料")
    @PutMapping("/profile")
    public Result<Void> updateUserProfile(@RequestBody UserProfileUpdateRequestDTO dto) {
        userService.updateUserProfile(dto);
        return Results.success();
    }

    @Operation(summary = "个人资料管理-上传/更新头像", description = "上传/更新头像")
    @PutMapping("/avatar")
    public Result<Void> setProfileAvatar(@Parameter(description = "文件id", required = true) @RequestBody AvatarUpdateRequestDTO dto) {
        userService.setProfileAvatar(dto.getFileId());
        return Results.success();
    }

    @PutMapping("/password")
    @Operation(summary = "个人资料管理-修改密码",description = "修改密码")
    public Result<Void> changePassword(@RequestBody ChangePasswordRequestDTO dto) {
        userService.changePassword(dto);
        return Results.success();
    }
}
