package cn.novalon.gym.manage.sys.config;

import cn.novalon.gym.manage.sys.core.service.impl.SysExceptionLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

/**
 * 异常日志配置类
 * 
 * @author 张翔
 * @date 2026-04-15
 */
@Configuration
public class ExceptionLogConfig {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionLogConfig.class);

    /**
     * 配置异常日志的路由
     */
    @Bean
    public RouterFunction<ServerResponse> exceptionLogRoutes(SysExceptionLogService exceptionLogService) {
        logger.info("配置异常日志路由");
        
        return route()
                .GET("/api/exception-logs", request -> 
                    ServerResponse.ok().body(exceptionLogService.findAll(), cn.novalon.gym.manage.sys.core.domain.SysExceptionLog.class))
                .GET("/api/exception-logs/{id}", request -> {
                    Long id = Long.valueOf(request.pathVariable("id"));
                    return exceptionLogService.findById(id)
                            .flatMap(log -> ServerResponse.ok().bodyValue(log))
                            .switchIfEmpty(ServerResponse.notFound().build());
                })
                .GET("/api/exception-logs/username/{username}", request -> {
                    String username = request.pathVariable("username");
                    return ServerResponse.ok().body(exceptionLogService.findByUsername(username), 
                            cn.novalon.gym.manage.sys.core.domain.SysExceptionLog.class);
                })
                .build();
    }
}