package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.export.model.ExportTaskInfo;
import com.writeoff.module.system.model.OperationAuditLogInfo;
import com.writeoff.module.system.service.OperationAuditLogService;
import javax.validation.constraints.NotBlank;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/audit-logs")
public class AuditLogController {
    private final OperationAuditLogService operationAuditLogService;

    public AuditLogController(OperationAuditLogService operationAuditLogService) {
        this.operationAuditLogService = operationAuditLogService;
    }

    @GetMapping
    @RequirePermission(value = "audit.log.read", dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "AUDIT_LOG_LIST")
    public ApiResponse<PageResult<OperationAuditLogInfo>> list(
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "actionCode", required = false) String actionCode,
            @RequestParam(value = "pageNo", required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        return ApiResponse.success(operationAuditLogService.list(userId, actionCode, pageNo, pageSize));
    }

    @GetMapping("/export")
    @RequirePermission(value = "audit.log.read", dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "AUDIT_LOG_EXPORT")
    public ApiResponse<PageResult<OperationAuditLogInfo>> export(
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "actionCode", required = false) String actionCode) {
        return ApiResponse.success(operationAuditLogService.list(userId, actionCode, 1, 500));
    }

    @GetMapping("/export-tasks")
    @RequirePermission(value = "audit.log.read", dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "AUDIT_LOG_EXPORT_TASKS")
    public ApiResponse<PageResult<ExportTaskInfo>> exportTasks() {
        return ApiResponse.success(operationAuditLogService.listExportTasks());
    }

    @PostMapping("/export-tasks")
    @RequirePermission(value = "audit.log.read", dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "AUDIT_LOG_EXPORT_TASK_CREATE")
    public ApiResponse<java.util.Map<String, Object>> createExportTask(@RequestBody AuditLogExportTaskRequest request) {
        return ApiResponse.success(operationAuditLogService.createExportTask(
                request.getUserId(),
                request.getActionCode(),
                request.getIdempotencyKey(),
                request.getFileName()
        ));
    }

    public static class AuditLogExportTaskRequest {
        @NotBlank(message = "幂等键不能为空")
        private String idempotencyKey;
        private Long userId;
        private String actionCode;
        private String fileName;

        public String getIdempotencyKey() { return idempotencyKey; }
        public void setIdempotencyKey(String idempotencyKey) { this.idempotencyKey = idempotencyKey; }
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        public String getActionCode() { return actionCode; }
        public void setActionCode(String actionCode) { this.actionCode = actionCode; }
        public String getFileName() { return fileName; }
        public void setFileName(String fileName) { this.fileName = fileName; }
    }
}
