package com.ichangzuo.module.auth.controller;

import com.ichangzuo.module.auth.service.AuthService;
import com.ichangzuo.common.enums.LogModuleEnum;
import com.ichangzuo.common.result.Result;
import com.ichangzuo.system.model.dto.CaptchaResult;
import com.ichangzuo.system.model.dto.LoginResult;
import com.ichangzuo.common.annotation.Log;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制层
 *
 * @author Ray
 * @since 2022/10/16
 */
@Tag(name = "01.认证中心")
@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
@Slf4j
public class AuthController {

    private final AuthService authService;

    @Operation(summary = "登录")
    @PostMapping("/login")
    @Log(value = "登录", module = LogModuleEnum.LOGIN)
    public Result<LoginResult> login(
            @Parameter(description = "用户名", example = "admin") @RequestParam String username,
            @Parameter(description = "密码", example = "123456") @RequestParam String password
    ) {
        LoginResult loginResult = authService.login(username, password);
        return Result.success(loginResult);
    }

    @Operation(summary = "注销")
    @DeleteMapping("/logout")
    @Log(value = "注销", module = LogModuleEnum.LOGIN)
    public Result<?> logout() {
        authService.logout();
        return Result.success();
    }

    @Operation(summary = "获取验证码")
    @GetMapping("/captcha")
    public Result<CaptchaResult> getCaptcha() {
        CaptchaResult captcha = authService.getCaptcha();
        return Result.success(captcha);
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public Result<?> register(
            @Parameter(description = "用户名", example = "admin") @RequestParam String username,
            @Parameter(description = "密码", example = "123456") @RequestParam String password,
            @Parameter(description = "验证码", example = "123456") @RequestParam String verifyCode
    ) {
        return authService.register(username,password,verifyCode);
    }

    @Operation(summary = "发送注册验证码")
    @PostMapping("/sendRegCode")
    public Result<?> sendRegCode(
            @Parameter(description = "联系方式（手机号码或邮箱地址）", required = true) @RequestParam String contact
    ) {
        return authService.sendRegCode(contact);
    }

    @Operation(summary = "核验注册验证码")
    @PostMapping("/verifyRegCode")
    public Result<?> verifyRegCode(
            @Parameter(description = "用户名", example = "admin") @RequestParam String username,
            @Parameter(description = "验证码", example = "123456") @RequestParam String verifyCode
    ) {
        return authService.verifyRegCode(username, verifyCode);
    }

    @Operation(summary = "自动登录")
    @PostMapping("/autoLogin")
    @Log(value = "自动登录", module = LogModuleEnum.LOGIN)
    public Result<?> autoLogin(
            @Parameter(description = "用户名", example = "admin") @RequestParam String username,
            @Parameter(description = "密码", example = "123456") @RequestParam String password
    ){
        return authService.autoLogin(username, password);
    }
}
