package cn.novalon.manage.sys.handler.log;

import cn.novalon.manage.sys.core.domain.OperationLog;
import cn.novalon.manage.sys.core.query.OperationLogQuery;
import cn.novalon.manage.sys.core.service.IOperationLogService;
import cn.novalon.manage.common.dto.PageRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * 操作日志处理器
 * 
 * 文件定义：处理操作日志相关的HTTP请求
 * 涉及业务：操作日志查询、分页、统计
 * 算法：使用WebFlux函数式编程模型处理响应式请求
 * 
 * @author 张翔
 * @date 2026-03-18
 */
@Component
@Tag(name = "操作日志", description = "操作日志相关操作")
public class OperationLogHandler {

    private final IOperationLogService logService;

    public OperationLogHandler(IOperationLogService logService) {
        this.logService = logService;
    }

    @Operation(summary = "获取所有操作日志", description = "获取系统中所有操作日志列表")
    public Mono<ServerResponse> getAllOperationLogs(ServerRequest request) {
        return ServerResponse.ok()
                .body(logService.findAll(), OperationLog.class);
    }

    @Operation(summary = "根据ID获取操作日志", description = "根据操作日志ID获取详细信息")
    public Mono<ServerResponse> getOperationLogById(ServerRequest request) {
        Long id = Long.valueOf(request.pathVariable("id"));
        return logService.findById(id)
                .flatMap(log -> ServerResponse.ok().bodyValue(log))
                .switchIfEmpty(ServerResponse.notFound().build());
    }

    @Operation(summary = "分页获取操作日志", description = "根据分页参数获取操作日志列表")
    public Mono<ServerResponse> getOperationLogsByPage(ServerRequest request) {
        int page = Integer.parseInt(request.queryParam("page").orElse("0"));
        int size = Integer.parseInt(request.queryParam("size").orElse("10"));
        String sort = request.queryParam("sort").orElse("createdAt");
        String order = request.queryParam("order").orElse("desc");
        String keyword = request.queryParam("keyword").orElse(null);
        String username = request.queryParam("username").orElse(null);
        String operation = request.queryParam("operation").orElse(null);
        String status = request.queryParam("status").orElse(null);

        PageRequest pageRequest = new PageRequest();
        pageRequest.setPage(page);
        pageRequest.setSize(size);
        pageRequest.setSort(sort);
        pageRequest.setOrder(order);
        pageRequest.setKeyword(keyword);

        OperationLogQuery query = new OperationLogQuery();
        query.setUsername(username);
        query.setOperation(operation);
        query.setStatus(status);
        query.setKeyword(keyword);

        return logService.findByQueryWithPagination(query, pageRequest)
                .flatMap(response -> ServerResponse.ok().bodyValue(response));
    }

    @Operation(summary = "获取操作日志总数", description = "获取系统中操作日志总数")
    public Mono<ServerResponse> getOperationLogCount(ServerRequest request) {
        return logService.count()
                .flatMap(count -> ServerResponse.ok().bodyValue(count));
    }

    @Operation(summary = "创建操作日志", description = "手动创建操作日志")
    public Mono<ServerResponse> createOperationLog(ServerRequest request) {
        return request.bodyToMono(OperationLog.class)
                .flatMap(logService::save)
                .flatMap(log -> ServerResponse.status(HttpStatus.CREATED).bodyValue(log));
    }
}