package com.agileboot.admin.controller.app;

import com.agileboot.admin.customize.service.login.LoginService;
import com.agileboot.admin.customize.service.login.command.LoginCommand;
import com.agileboot.admin.customize.service.login.dto.CaptchaDTO;
import com.agileboot.admin.customize.service.login.dto.ConfigDTO;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.domain.common.dto.CurrentLoginUserDTO;
import com.agileboot.domain.common.dto.TokenDTO;
import com.agileboot.domain.system.user.UserApplicationService;
import com.agileboot.domain.system.user.command.RegisterUserCommand;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.web.SystemLoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author codex
 */
@Tag(name = "小程序登录API", description = "小程序登录注册相关接口")
@RestController
@RequestMapping("/app")
@RequiredArgsConstructor
public class AppAuthController {

    private final LoginService loginService;
    private final UserApplicationService userApplicationService;

    @Operation(summary = "小程序配置")
    @GetMapping("/getConfig")
    public ResponseDTO<ConfigDTO> getConfig() {
        return ResponseDTO.ok(loginService.getConfig());
    }

    @Operation(summary = "小程序验证码")
    @GetMapping("/captchaImage")
    public ResponseDTO<CaptchaDTO> getCaptchaImg() {
        return ResponseDTO.ok(loginService.generateCaptchaImg());
    }

    @Operation(summary = "小程序登录")
    @PostMapping("/login")
    public ResponseDTO<TokenDTO> login(@RequestBody LoginCommand command) {
        String token = loginService.login(command);
        return ResponseDTO.ok(buildTokenDTO(token));
    }

    @Operation(summary = "小程序注册")
    @PostMapping("/register")
    public ResponseDTO<TokenDTO> register(@Validated @RequestBody RegisterUserCommand command) {
        decryptRegisterPassword(command);
        loginService.validateCaptchaIfEnabled(command.getUsername(), command.getCaptchaCode(),
            command.getCaptchaCodeKey());
        userApplicationService.registerUser(command);
        loginService.recordRegisterInfo(command.getUsername());
        return ResponseDTO.ok(buildTokenDTO(loginService.createTokenForRegisteredUser(command.getUsername())));
    }

    @Operation(summary = "小程序当前登录用户")
    @GetMapping("/getLoginUserInfo")
    public ResponseDTO<CurrentLoginUserDTO> getLoginUserInfo() {
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        return ResponseDTO.ok(userApplicationService.getLoginUserInfo(loginUser));
    }

    private TokenDTO buildTokenDTO(String token) {
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        CurrentLoginUserDTO currentUser = userApplicationService.getLoginUserInfo(loginUser);
        return new TokenDTO(token, currentUser);
    }

    private void decryptRegisterPassword(RegisterUserCommand command) {
        command.setPassword(loginService.decryptPassword(command.getPassword()));
        command.setConfirmPassword(loginService.decryptPassword(command.getConfirmPassword()));
    }

}
