package com.sikey.wa04.api.user.client.rest;

import com.sikey.wa04.business.application.service.UcloudService;
import com.sikey.wa04.business.application.service.user.AuthenticateService;
import com.sikey.wa04.business.application.service.user.UserService;
import com.sikey.wa04.business.infrastructure.configuration.ProjectContextConstant;
import com.sikey.wa04.business.infrastructure.dto.request.user.*;
import com.sikey.wa04.business.infrastructure.dto.response.user.RegisterOrNotResponse;
import com.sikey.wa04.business.infrastructure.dto.response.user.SignInResponse;
import com.sikey.wa04.common.exception.errors.Errors;
import com.sikey.wa04.common.interceptor.Authenticated;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(ProjectContextConstant.USER_CONTEXT_URL + "/user")
@Slf4j
public class UserController {

    @Resource
    private AuthenticateService authenticateService;

    @Resource
    private UserService userService;

    @Resource
    private UcloudService ucloudService;

    @PostMapping("/sign-in")
    public SignInResponse signIn(@Valid @RequestBody SignInRequest request) {
        if (!request.isEmailMode()) {
            if (!ucloudService.validation(request.phoneNumber(), request.areaCode(), request.captcha())) {
                throw Errors.VERIFICATION_CODE_IS_INCORRECT.getException();
            }
        }
        return authenticateService.signIn(request);
    }

    @PostMapping("/sign-up")
    public void signUp(@Valid @RequestBody SignUpRequest request) {
        if (!request.isEmailMode()) {
            if (!ucloudService.validation(request.phoneNumber(), request.areaCode(), request.captcha())) {
                throw Errors.VERIFICATION_CODE_IS_INCORRECT.getException();
            }
        }
        authenticateService.signUp(request);
    }

    @PostMapping("/sms-code")
    public void smsCode(@Valid @RequestBody SMSCodeRequest request) {
        ucloudService.sendSMSCode(request.phoneNumber(), request.areaCode());
    }

    @Authenticated
    @PostMapping("/unregister")
    public void unregister(@Valid @RequestBody UnregisterRequest request) throws Exception {
        userService.unregister(request);
    }

    @Authenticated
    @PostMapping("/reset-password")
    public void resetPassword(@Valid @RequestBody ResetPasswordRequest request) throws Exception {
        userService.resetPassword(request);
    }

    @PostMapping("/retrieve-password")
    public void retrievePassword(@Valid @RequestBody RetrievePasswordRequest request) throws Exception {
        userService.retrievePassword(request);
    }

    @PostMapping("/update-password")
    public void updatePassword(@Valid @RequestBody UpdatePasswordRequest request) throws Exception {
        userService.updatePassword(request);
    }

    /**
     * 是否注册
     *
     * @param smsCodeRequest 注册
     * @return
     */
    @GetMapping("/registerOrNot")
    public RegisterOrNotResponse registerOrNot(@Valid SMSCodeRequest smsCodeRequest) {
        return userService.registerOrNot(smsCodeRequest);
    }

}
