package com.erp.task.controller;

import com.erp.common.core.model.PageResponse;
import com.erp.task.dto.TaskExecutionLogDTO;
import com.erp.task.service.TaskExecutionLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
 * 任务执行记录接口
 */
@Tag(name = "任务执行记录", description = "任务执行日志查询")
@RestController
@RequestMapping("/api/task/log")
@RequiredArgsConstructor
public class TaskExecutionLogController {

    private final TaskExecutionLogService logService;

    @Operation(summary = "查询任务的执行记录")
    @GetMapping("/task/{taskId}")
    public ResponseEntity<PageResponse<TaskExecutionLogDTO>> findByTaskId(
            @PathVariable Long taskId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        return ResponseEntity.ok(logService.findByTaskId(taskId, page, pageSize));
    }

    @Operation(summary = "查询所有执行记录")
    @GetMapping("/list")
    public ResponseEntity<PageResponse<TaskExecutionLogDTO>> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        return ResponseEntity.ok(logService.findAll(page, pageSize));
    }

    @Operation(summary = "条件查询执行记录")
    @GetMapping("/query")
    public ResponseEntity<PageResponse<TaskExecutionLogDTO>> query(
            @Parameter(description = "任务ID") @RequestParam(required = false) Long taskId,
            @Parameter(description = "执行状态") @RequestParam(required = false) String status,
            @Parameter(description = "开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        return ResponseEntity.ok(logService.query(taskId, status, startTime, endTime, page, pageSize));
    }

    @Operation(summary = "根据批次号查询")
    @GetMapping("/batch/{batchNo}")
    public ResponseEntity<TaskExecutionLogDTO> findByBatchNo(@PathVariable String batchNo) {
        return logService.findByBatchNo(batchNo)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @Operation(summary = "查询可重试的失败记录")
    @GetMapping("/retryable")
    public ResponseEntity<List<TaskExecutionLogDTO>> findRetryableFailures(
            @RequestParam(defaultValue = "3") Integer maxRetries) {
        return ResponseEntity.ok(logService.findRetryableFailures(maxRetries));
    }

    @Operation(summary = "删除旧日志")
    @DeleteMapping("/cleanup")
    public ResponseEntity<Map<String, Object>> cleanupOldLogs(
            @RequestParam(defaultValue = "30") int days) {
        int deleted = logService.deleteOldLogs(days);
        return ResponseEntity.ok(Map.of(
                "deleted", deleted,
                "days", days
        ));
    }

    @Operation(summary = "获取今日统计")
    @GetMapping("/stats/today")
    public ResponseEntity<Map<String, Long>> getTodayStats() {
        return ResponseEntity.ok(logService.getStatsToday());
    }
}
