package com.haha.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haha.admin.annotation.RequirePermission;
import com.haha.common.annotation.Log;
import com.haha.common.enums.OperationType;
import com.haha.common.vo.Result;
import com.haha.entity.OperationLog;
import com.haha.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 操作日志控制器
 */
@Slf4j
@RestController
@RequestMapping("/operation-log")
@RequiredArgsConstructor
public class OperationLogController {

    private final OperationLogService operationLogService;

    /**
     * 分页查询操作日志列表
     */
    @RequirePermission("operation-log:read")
    @GetMapping("/list")
    public Result<Map<String, Object>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String module,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        Page<OperationLog> pageParam = new Page<>(page, pageSize);
        Page<OperationLog> result = operationLogService.pageList(pageParam, module, username, status, startTime, endTime);
        
        Map<String, Object> data = Map.of(
                "list", result.getRecords(),
                "total", result.getTotal(),
                "page", result.getCurrent(),
                "pageSize", result.getSize()
        );
        
        return Result.success(data);
    }

    /**
     * 获取操作日志详情
     */
    @RequirePermission("operation-log:read")
    @GetMapping("/{id}")
    public Result<OperationLog> detail(@PathVariable Long id) {
        OperationLog operationLog = operationLogService.getById(id);
        if (operationLog == null) {
            return Result.error("日志不存在");
        }
        return Result.success(operationLog);
    }

    /**
     * 批量删除操作日志
     */
    @RequirePermission("operation-log:delete")
    @Log(module = "系统监控", operation = OperationType.DELETE, summary = "批量删除操作日志")
    @DeleteMapping("/batch")
    public Result<Void> deleteBatch(@RequestBody List<Long> ids) {
        operationLogService.deleteBatch(ids);
        return Result.success("删除成功", null);
    }

    /**
     * 删除单条操作日志
     */
    @RequirePermission("operation-log:delete")
    @Log(module = "系统监控", operation = OperationType.DELETE, summary = "删除操作日志")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        operationLogService.deleteBatch(List.of(id));
        return Result.success("删除成功", null);
    }

    /**
     * 清空所有操作日志
     */
    @RequirePermission("operation-log:delete")
    @Log(module = "系统监控", operation = OperationType.DELETE, summary = "清空操作日志")
    @DeleteMapping("/clear")
    public Result<Void> clearAll() {
        operationLogService.clearAll();
        return Result.success("清空成功", null);
    }

    /**
     * 清空指定时间之前的日志
     */
    @RequirePermission("operation-log:delete")
    @Log(module = "系统监控", operation = OperationType.DELETE, summary = "清空历史操作日志")
    @DeleteMapping("/clear-before")
    public Result<Void> clearBeforeTime(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beforeTime) {
        operationLogService.clearBeforeTime(beforeTime);
        return Result.success("清空成功", null);
    }

    /**
     * 获取操作日志统计数据
     */
    @RequirePermission("operation-log:read")
    @GetMapping("/statistics")
    public Result<Map<String, Object>> statistics() {
        Map<String, Object> data = operationLogService.getStatistics();
        return Result.success(data);
    }
}
