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.AccountRegisterDto;
import cn.reghao.tnb.auth.app.model.dto.PasswordResetDto;
import cn.reghao.tnb.auth.app.model.po.UserRegistry;
import cn.reghao.tnb.auth.app.service.AccountRegistryService;
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.*;

/**
 * 账号注册接口
 *
 * @author reghao
 * @date 2022-04-13 10:04:41
 */
@Tag(name = "账号注册接口")
@RestController
@RequestMapping("/api/auth")
public class AccountRegistryController {
    private final AccountRegistryService accountRegistryService;
    private final AccountProfileService accountProfileService;

    public AccountRegistryController(AccountRegistryService accountRegistryService,
                                     AccountProfileService accountProfileService) {
        this.accountRegistryService = accountRegistryService;
        this.accountProfileService = accountProfileService;
    }

    @Operation(summary = "获取注册开放状态", description = "N")
    @GetMapping(value = "/registry", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getRegistryStatus() {
        UserRegistry userRegistry = accountRegistryService.getUserRegistry();
        return WebResult.success(userRegistry.getEnabled());
    }

    @Operation(summary = "注册用户", description = "N")
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public String signup(@RequestBody @Validated AccountRegisterDto accountRegisterDto) {
        Result result = accountRegistryService.createAccount(accountRegisterDto);
        return WebResult.result(result);
    }

    @Operation(summary = "重置密码", description = "N")
    @PostMapping(value = "/forgot", produces = MediaType.APPLICATION_JSON_VALUE)
    public String forgotPassword(@RequestBody @Validated PasswordResetDto passwordResetDto) {
        Result result = accountProfileService.resetPassword(passwordResetDto);
        return WebResult.result(result);
    }
}
