package tech.easyflow.admin.controller.auth;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Profile;
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.config.DevLoginGuard;
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;

@Profile("dev")
@RestController
@RequestMapping("/api/v1/auth/")
@ConditionalOnProperty(prefix = "easyflow.login.dev-bypass", name = "enabled", havingValue = "true")
public class DevLoginController {

    private final AuthService authService;
    private final DevLoginGuard devLoginGuard;

    public DevLoginController(AuthService authService, DevLoginGuard devLoginGuard) {
        this.authService = authService;
        this.devLoginGuard = devLoginGuard;
    }

    @SaIgnore
    @PostMapping("dev-login")
    public Result<LoginVO> devLogin(HttpServletRequest request,
                                    @JsonBody(value = "account", required = true) String account) {
        devLoginGuard.checkAccess(request, account);
        return Result.ok(authService.devLogin(account));
    }
}
