package com.seer.teach.user.app.auth.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogOperation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.ModuleEnum;
import com.seer.teach.user.app.auth.request.LoginParam;
import com.seer.teach.user.app.auth.response.LoginResp;
import com.seer.teach.user.app.auth.service.AppAuthService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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;

@EncryptionAnnotation
@DecryptionAnnotation
@RequestMapping("/app/auth")
@RestController
@Tag(name = "App 用户 - 认证")
@Validated
@RequiredArgsConstructor
public class AppAuthController {


    @Autowired
    private AppAuthService userAuthService;

    @PostMapping("/login")
    public ResultBean<LoginResp> login(@RequestBody LoginParam request) {
        return ResultBean.success(userAuthService.authenticate(request));
    }

    @Operation(summary = "登出")
    @PostMapping("/logout")
    @SaCheckLogin(type = StpUtil.TYPE)
    @LogPrint
    @LogOperation(module = ModuleEnum.SEER_USER, subModule = "登出")
    @SaCheckPermission("app:user:logout")
    public ResultBean<String> logout() {
        StpUtil.logout();
        return ResultBean.success("登出成功");
    }
}