package com.lesingle.modules.sys.controller;

import com.lesingle.common.result.PageResult;
import com.lesingle.common.result.Result;
import com.lesingle.modules.sys.dto.QueryLogDto;
import com.lesingle.modules.sys.service.ISysLogService;
import com.lesingle.security.annotation.RequirePermission;
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 = "系统日志")
@RestController
@RequestMapping("/logs")
@RequiredArgsConstructor
public class SysLogController {

    private final ISysLogService logService;

    @GetMapping
    @RequirePermission("log:read")
    @Operation(summary = "查询日志列表")
    public Result<PageResult<Map<String, Object>>> findAll(QueryLogDto dto) {
        return Result.success(logService.findAll(dto));
    }

    @GetMapping("/statistics")
    @RequirePermission("log:read")
    @Operation(summary = "日志统计")
    public Result<Map<String, Object>> getStatistics(@RequestParam(required = false) Integer days) {
        return Result.success(logService.getStatistics(days));
    }

    @GetMapping("/{id}")
    @RequirePermission("log:read")
    @Operation(summary = "查询日志详情")
    public Result<Map<String, Object>> findOne(@PathVariable Long id) {
        return Result.success(logService.findDetail(id));
    }

    @DeleteMapping
    @RequirePermission("log:delete")
    @Operation(summary = "批量删除日志")
    public Result<Void> batchDelete(@RequestBody Map<String, List<Long>> body) {
        logService.batchDelete(body.get("ids"));
        return Result.success();
    }

    @PostMapping("/clean")
    @RequirePermission("log:delete")
    @Operation(summary = "清理旧日志")
    public Result<Map<String, Object>> cleanOldLogs(@RequestBody(required = false) Map<String, Integer> body) {
        Integer daysToKeep = body != null ? body.get("daysToKeep") : null;
        return Result.success(logService.cleanOldLogs(daysToKeep));
    }
}
