package com.luojia_channel.modules.user.controller;

import com.luojia_channel.common.domain.Result;
import com.luojia_channel.modules.user.dto.UserChangeInfoDTO;
import com.luojia_channel.modules.user.vo.UserInfoVO;
import com.luojia_channel.modules.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/user/info")
@RequiredArgsConstructor
@Tag(name = "用户信息管理", description = "用户修改个人信息相关接口")
public class UserInfoController {
    private final UserInfoService userInfoService;
    
    @GetMapping("/getuserinfo")
    @Operation(
            summary="获取用户信息",
            description="获取指定用户的信息，如果不指定userId则获取当前登录用户信息",
            tags = {"用户信息管理"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "获取失败，用户不存在或未登录")
    })
    public Result<UserInfoVO> getUserInfo(@RequestParam(required = false) Long userId) {
        return Result.success(userInfoService.getUserInfo(userId));
    }
    
    @PostMapping("/update")
    @Operation(
            summary="修改用户信息",
            tags = {"用户信息管理"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "500", description = "修改失败，请稍后重试")
    })
    public Result<Void> updateInfo(@RequestBody UserChangeInfoDTO userChangeInfoDTO){
        userInfoService.updateInfo(userChangeInfoDTO);
        return Result.success();
    }

    @PostMapping("/password")
    @Operation(
            summary="用户修改密码",
            tags = {"用户信息管理"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "500", description = "修改失败，请稍后重试")
    })
    public Result<Void> updatePassword(@RequestParam String password){
        userInfoService.updatePassword(password);
        return Result.success();
    }

    @PostMapping("/avatar")
    @Operation(
            summary="用户上传头像",
            tags = {"用户信息管理"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "上传成功"),
            @ApiResponse(responseCode = "500", description = "上传失败，请稍后重试")
    })
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        return Result.success(userInfoService.uploadAvatar(file));
    }
}
