package com.reading.platform.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reading.platform.common.annotation.RequireRole;
import com.reading.platform.common.enums.UserRole;
import com.reading.platform.common.response.PageResult;
import com.reading.platform.common.response.Result;
import com.reading.platform.entity.OperationLog;
import com.reading.platform.service.OperationLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "管理员 - 操作日志", description = "操作日志管理接口（管理员专用）")
@RestController
@RequestMapping("/api/v1/admin/operation-logs")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminOperationLogController {

    private final OperationLogService operationLogService;

    @Operation(summary = "获取操作日志")
    @GetMapping
    public Result<PageResult<OperationLog>> getLogs(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize,
            @RequestParam(required = false) String tenantId,
            @RequestParam(required = false) String module,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        Page<OperationLog> page = operationLogService.getLogs(pageNum, pageSize, tenantId, module, startDate, endDate);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "获取按模块统计（操作日志）")
    @GetMapping("/stats")
    public Result<List<Map<String, Object>>> getStats(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return Result.success(operationLogService.getModuleStats(null, startDate, endDate));
    }

    @Operation(summary = "根据 ID 获取操作日志")
    @GetMapping("/{id}")
    public Result<OperationLog> getLogById(@PathVariable String id) {
        return Result.success(operationLogService.getLogById(id));
    }
}
