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.AuditLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

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

/**
 * 审计日志控制器。
 * <p>提供登录日志、操作日志、异常日志的查询及溯源 RESTful API。</p>
 */
@Tag(name = "审计日志", description = "登录日志、操作日志、异常日志的查询与溯源")
@RestController
@RequestMapping("/api/log")
public class AuditLogController {

    private final AuditLogService auditLogService;

    public AuditLogController(AuditLogService auditLogService) {
        this.auditLogService = auditLogService;
    }

    /**
     * 查询登录日志。
     *
     * @requirement(name="登录日志", id="SRS-F-05-001")
     * @requirement(name="支持溯源查询", id="SRS-F-05-004")
     */
    @Operation(summary = "查询登录日志")
    @GetMapping("/login")
    public ApiResult<List<LoginLog>> loginLogs(
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate) {
        return ApiResult.success(auditLogService.queryLoginLogs(username, startDate, endDate));
    }

    /**
     * 查询操作日志。
     *
     * @requirement(name="操作日志", id="SRS-F-05-002")
     * @requirement(name="支持溯源查询", id="SRS-F-05-004")
     */
    @Operation(summary = "查询操作日志")
    @GetMapping("/operation")
    public ApiResult<List<OperationLog>> operationLogs(
            @Parameter(description = "操作人") @RequestParam(required = false) String operator,
            @Parameter(description = "操作类型") @RequestParam(required = false) String operateType,
            @Parameter(description = "业务类型") @RequestParam(required = false) String businessType) {
        return ApiResult.success(auditLogService.queryOperationLogs(operator, operateType, businessType));
    }

    /**
     * 查询异常日志。
     *
     * @requirement(name="异常日志留存", id="SRS-F-05-003")
     * @requirement(name="支持溯源查询", id="SRS-F-05-004")
     */
    @Operation(summary = "查询异常日志")
    @GetMapping("/exception")
    public ApiResult<List<ExceptionLog>> exceptionLogs(
            @Parameter(description = "异常类型") @RequestParam(required = false) String exceptionType,
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate) {
        return ApiResult.success(auditLogService.queryExceptionLogs(exceptionType, startDate, endDate));
    }

    /**
     * 全链路溯源查询。
     *
     * @requirement(name="支持溯源查询", id="SRS-F-05-004")
     */
    @Operation(summary = "溯源查询")
    @PostMapping("/trace")
    public ApiResult<Map<String, Object>> trace(
            @Parameter(description = "溯源标识") @RequestParam String traceId) {
        return ApiResult.success(auditLogService.trace(traceId));
    }
}
