package com.example.demo.controller;

import com.example.demo.model.ApiResult;
import com.example.demo.model.ExceptionLog;
import com.example.demo.model.LoginLog;
import com.example.demo.model.OperationLog;
import com.example.demo.service.LogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

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

/**
 * 审计日志控制器。
 * <p>提供登录日志、操作日志、异常日志查询及溯源查询接口。</p>
 */
@RestController
@RequestMapping("/api/log")
@Tag(name = "审计日志", description = "审计日志模块接口（需求标识：SRS-F-05-001~SRS-F-05-004）")
public class LogController {

    private final LogService logService;

    public LogController(LogService logService) {
        this.logService = logService;
    }

    /**
     * 查询登录日志。
     *
     * @requirement(name="登录日志", id="SRS-F-05-001")
     */
    @GetMapping("/login")
    @Operation(summary = "查询登录日志", description = "按用户名、日期范围查询登录日志")
    public ApiResult<List<LoginLog>> loginLogs(@RequestParam(required = false) String username,
                                                @RequestParam(required = false) String dateFrom,
                                                @RequestParam(required = false) String dateTo) {
        return ApiResult.success(logService.listLoginLogs(username, dateFrom, dateTo));
    }

    /**
     * 查询操作日志。
     *
     * @requirement(name="操作日志", id="SRS-F-05-002")
     */
    @GetMapping("/operation")
    @Operation(summary = "查询操作日志", description = "按操作人、操作类型、日期范围查询操作日志（含数据对比）")
    public ApiResult<List<OperationLog>> operationLogs(@RequestParam(required = false) String operator,
                                                        @RequestParam(required = false) String operationType,
                                                        @RequestParam(required = false) String dateFrom,
                                                        @RequestParam(required = false) String dateTo) {
        return ApiResult.success(logService.listOperationLogs(operator, operationType, dateFrom, dateTo));
    }

    /**
     * 查询异常日志。
     *
     * @requirement(name="异常日志留存", id="SRS-F-05-003")
     */
    @GetMapping("/exception")
    @Operation(summary = "查询异常日志", description = "按异常类型、日期范围查询异常日志")
    public ApiResult<List<ExceptionLog>> exceptionLogs(@RequestParam(required = false) String exceptionType,
                                                        @RequestParam(required = false) String dateFrom,
                                                        @RequestParam(required = false) String dateTo) {
        return ApiResult.success(logService.listExceptionLogs(exceptionType, dateFrom, dateTo));
    }

    /**
     * 溯源查询。
     *
     * @requirement(name="支持溯源查询", id="SRS-F-05-004")
     */
    @PostMapping("/trace")
    @Operation(summary = "溯源查询", description = "按关键字、日期范围检索审计日志")
    public ApiResult<Map<String, Object>> trace(@RequestParam(required = false) String keyword,
                                                 @RequestParam(required = false) String dateFrom,
                                                 @RequestParam(required = false) String dateTo) {
        return ApiResult.success(logService.traceQuery(keyword, dateFrom, dateTo));
    }
}
