package tech.easyflow.admin.controller.auth;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.easyflow.auth.entity.LoginDTO;
import tech.easyflow.auth.entity.LoginVO;
import tech.easyflow.auth.service.AuthService;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.web.jsonbody.JsonBody;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/auth/")
public class AuthController {

    @Resource
    private AuthService authService;

    @PostMapping("login")
    public Result<LoginVO> login(@JsonBody LoginDTO loginDTO) {
        LoginVO res = authService.login(loginDTO);
        return Result.ok(res);
    }

    @PostMapping("loginByApiKey")
    @SaIgnore
    public Result<LoginVO> loginByApiKey(@JsonBody(value = "apiKey", required = true) String apiKey) {
        return Result.ok(authService.loginByApiKey(apiKey));
    }

    @PostMapping("logout")
    public Result<Void> logout() {
        StpUtil.logout();
        return Result.ok();
    }

    @GetMapping("getPermissions")
    public Result<List<String>> getPermissions() {
        List<String> permissionList = StpUtil.getPermissionList();
        return Result.ok(permissionList);
    }
}
