package com.backendsys.modules.system.controller;

import com.backendsys.aspect.RateLimiting;
import com.backendsys.modules.common.aspect.SysLog;
import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.utils.Result;
import com.backendsys.modules.system.entity.SysAuth;
import com.backendsys.modules.system.entity.SysAuthPhone;
import com.backendsys.modules.system.entity.SysMobileArea;
import com.backendsys.modules.system.entity.SysUserDTO;
import com.backendsys.modules.system.service.SysAuthService;
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 = "系统登录")
public class SysAuthController {

    @Autowired
    private SysAuthService sysAuthService;

    // -------------------------------------------------------------------------------------------------

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

    @Operation(summary = "判断是否需验证码登录状态")
    @GetMapping("/api/system/auth/checkCaptchaRequired")
    public Result checkCaptchaRequired(String username) {
        return Result.success().put("data", sysAuthService.checkCaptchaRequired(username));
    }

    @Operation(summary = "系统用户登录 (用户名)")
    @PostMapping(value = "/api/system/auth/login")
    public Result systemLogin(@Validated(SysAuth.Login.class) @RequestBody SysAuth sysAuth) {
        return Result.success().put("data", sysAuthService.login(sysAuth));
    }



    // -------------------------------------------------------------------------------------------------

    @Anonymous
    @Operation(summary = "获取手机号地区列表")
    @RateLimiting(key = "getMobileAreaCode")
    @GetMapping("/api/system/getMobileAreaCode")
    public Result getMobileAreaCode(@Validated SysMobileArea sysMobileArea) {
        return Result.success().put("data", sysAuthService.getMobileAreaList(sysMobileArea));
    }
    @Operation(summary = "系统用户登录 (手机号码)")
    @PostMapping(value = "/api/system/auth/loginWithPhone")
    @RateLimiting(key = "systemLoginWithPhone")
    public Result systemLoginWithPhone(@Validated(SysAuthPhone.Login.class) @RequestBody SysAuthPhone sysAuthPhone) {
        return Result.success().put("data", sysAuthService.loginWithPhone(sysAuthPhone));
    }

    @Anonymous
    @Operation(summary = "系统用户注册 (用户名+手机号码)")
    @PostMapping("/api/system/auth/register")
    @RateLimiting(key = "registerUser", limit = 5)
    public Result register(@Validated(SysUserDTO.Register.class) @RequestBody SysUserDTO sysUserDTO) {
        return Result.success().put("data", sysAuthService.register(sysUserDTO));
    }

    @Anonymous
    @Operation(summary = "忘记密码")
    @PostMapping("/api/system/user/forgotPassword")
    public Result forgotPassword(@Validated(SysUserDTO.ForgotPassword.class) @RequestBody SysUserDTO sysUserDTO) {
        return Result.success().put("data", sysAuthService.forgotPassword(sysUserDTO));
    }

    @Operation(summary = "退出登录")
    @PostMapping(value = "/api/system/user/logout")
    public Result systemLogout() {
        return Result.success().put("data", sysAuthService.logout());
    }

}
