package cn.skcks.matrix.v2.api.test;

import cn.hutool.core.util.IdUtil;
import cn.skcks.matrix.v2.annotation.web.JsonMapping;
import cn.skcks.matrix.v2.annotation.web.auth.Auth;
import cn.skcks.matrix.v2.annotation.web.methods.GetJson;
import cn.skcks.matrix.v2.annotation.web.methods.PostJson;
import cn.skcks.matrix.v2.config.swagger.SwaggerConfig;
import cn.skcks.matrix.v2.model.jwt.dto.Claims;
import cn.skcks.matrix.v2.model.route.dto.RouteInfo;
import cn.skcks.matrix.v2.model.test.dto.GetUserParams;
import cn.skcks.matrix.v2.model.user.convert.UserConvertor;
import cn.skcks.matrix.v2.model.user.vo.UserVo;
import cn.skcks.matrix.v2.orm.mybatis.dynamic.model.User;
import cn.skcks.matrix.v2.services.casbin.CasbinService;
import cn.skcks.matrix.v2.services.jwt.JwtService;
import cn.skcks.matrix.v2.services.user.UserService;
import cn.skcks.matrix.v2.services.route.RouteService;
import cn.skcks.matrix.v2.utils.json.JsonResponse;
import cn.skcks.matrix.v2.utils.page.PageWrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "测试")
@RestController
@JsonMapping("/test")
@RequiredArgsConstructor
public class TestController {
    private final UserService userService;
    private final RouteService routeService;

    private final JwtService jwtService;

    private final CasbinService casbinService;

    @Bean
    public GroupedOpenApi testApi() {
        return SwaggerConfig.api("Test", "/test");
    }

    @Operation(summary = "查询用户表")
    @PostJson("/users")
    public JsonResponse<PageWrapper<UserVo>> users(@RequestBody @Validated GetUserParams params) {
        PageInfo<User> users = userService.getUsers(params.getPage(), params.getSize());
        return JsonResponse.success(PageWrapper.of(UserConvertor.INSTANCE.daoToVo(users)));
    }

    @JsonMapping(value = "/route/{*}", method = {RequestMethod.GET})
    public JsonResponse<RouteInfo> route(HttpServletRequest request) {
        return JsonResponse.success(routeService.getRouteInfo(request));
    }

    @Auth
    @JsonMapping(value = "/routes", method = {RequestMethod.GET})
    public JsonResponse<Object> routes(HttpServletRequest request) {
        return JsonResponse.success(routeService.getRoutes());
    }

    @GetJson("/getToken")
    public JsonResponse<String> getToken() {
        return JsonResponse.success(jwtService.generateToken(new Claims(IdUtil.fastUUID(), "test")));
    }

    @PostJson("/verifyToken")
    public JsonResponse<Boolean> verifyToken(@RequestParam String token) {
        return JsonResponse.success(jwtService.verifyToken(token));
    }

    @PostJson("/parseToken")
    public JsonResponse<Claims> parseToken(@RequestParam String token) {
        return JsonResponse.success(jwtService.parseToken(token));
    }

    @GetJson("/casbin")
    public JsonResponse<Boolean> casbinTest(@RequestParam String identify,
                                            @RequestParam String system,
                                            @RequestParam String url,
                                            @RequestParam String method) {
        return JsonResponse.success(casbinService.enforce(identify, system, url, method));
    }

    @PostJson("/casbin")
    @Operation(summary = "重新加载 casbin policy")
    public JsonResponse<Void> reloadCasbin(){
        casbinService.reload();
        return JsonResponse.success(null);
    }
}
