package com.backendsys.modules.app.user.controller;

import com.backendsys.modules.app.user.entity.AppAuth;
import com.backendsys.modules.app.user.service.AppAuthService;
import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.io.IOException;

@Validated
@RestController
@Tag(name = "APP用户登录与注册")
public class AppAuthController {

    @Autowired
    private AppAuthService appAuthService;

    @Anonymous
    @Operation(summary = "获取图形验证码")
    @GetMapping("/api/app/auth/getCaptcha")
    public void getCaptcha(HttpServletResponse response) throws IOException {
        appAuthService.renderCaptcha(response);
    }

    @Anonymous
    @Operation(summary = "前台用户登录 (手机账号)")
    @PostMapping("/api/app/auth/loginWithPhone")
    public Result login(@Validated(AppAuth.LoginWithPhone.class) @RequestBody AppAuth appAuth) {
        return Result.success().put("data", appAuthService.loginWithPhone(appAuth));
    }

    @Anonymous
    @Operation(summary = "前台用户登录 (微信小程序用户)")
    @PostMapping("/api/app/auth/loginWithWechatMiniprogram")
    public Result loginWithWechatMiniprogram(@Validated(AppAuth.LoginWithWechat.class) @RequestBody AppAuth appAuth) {
        return Result.success().put("data", appAuthService.loginWithWechatMiniprogram(appAuth));
    }

}
