package cn.reghao.tnb.auth.app.controller;

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.auth.app.model.dto.PasswordUpdateDto;
import cn.reghao.tnb.auth.app.model.dto.UserEmailUpdate;
import cn.reghao.tnb.auth.app.service.AccountProfileService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;

/**
 * @author reghao
 * @date 2023-10-28 22:34:47
 */
@Tag(name = "帐号管理接口")
@RestController
@RequestMapping("/api/account")
public class AccountProfileController {
    private final AccountProfileService accountProfileService;

    public AccountProfileController(AccountProfileService accountProfileService) {
        this.accountProfileService = accountProfileService;
    }

    @Operation(summary = "修改用户名", description = "N")
    @PostMapping(value = "/screenname", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateUserScreenName(@NotBlank(message = "用户名不能为空") String screenName) {
        Result result = accountProfileService.updateUserScreenName(screenName);
        return WebResult.result(result);
    }

    @Operation(summary = "修改手机号/邮箱", description = "N")
    @PostMapping(value = "/email", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateUserEmail(@RequestBody @Validated UserEmailUpdate userEmailUpdate) throws Exception {
        Result result = accountProfileService.updateUserEmail(userEmailUpdate);
        return WebResult.result(result);
    }

    @Operation(summary = "修改密码(已登入)", description = "N")
    @PostMapping(value = "/password", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateUserPassword(@RequestBody @Validated PasswordUpdateDto passwordUpdateDto) {
        Result result = accountProfileService.updatePassword(passwordUpdateDto);
        return WebResult.result(result);
    }

    @Operation(summary = "删除账号", description = "N")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteAccount() {
        return WebResult.success();
    }
}
