package com.qqchen.deploy.backend.schedule.api;

import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import com.qqchen.deploy.backend.schedule.dto.JobDashboardDTO;
import com.qqchen.deploy.backend.schedule.dto.ScheduleJobDTO;
import com.qqchen.deploy.backend.schedule.entity.ScheduleJob;
import com.qqchen.deploy.backend.schedule.query.ScheduleJobQuery;
import com.qqchen.deploy.backend.schedule.service.IScheduleJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时任务API控制器
 * 
 * @author qichen
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/schedule/jobs")
@Tag(name = "定时任务管理", description = "定时任务的增删改查及任务控制接口")
public class ScheduleJobApiController extends BaseController<ScheduleJob, ScheduleJobDTO, Long, ScheduleJobQuery> {

    @Resource
    private IScheduleJobService scheduleJobService;

    /**
     * 获取Dashboard数据
     */
    @Operation(summary = "获取Dashboard数据", description = "获取任务统计、正在执行的任务、最近执行日志")
    @GetMapping("/dashboard")
    public Response<JobDashboardDTO> getDashboard() {
        JobDashboardDTO dashboard = scheduleJobService.getDashboard();
        return Response.success(dashboard);
    }

    /**
     * 启动任务
     */
    @Operation(summary = "启动定时任务", description = "将任务加载到Quartz调度器并开始执行")
    @PostMapping("/{id}/start")
    public Response<Void> startJob(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id
    ) {
        scheduleJobService.startJob(id);
        return Response.success();
    }

    /**
     * 暂停任务
     */
    @Operation(summary = "暂停定时任务", description = "暂停正在运行的任务，保留调度信息")
    @PostMapping("/{id}/pause")
    public Response<Void> pauseJob(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id
    ) {
        scheduleJobService.pauseJob(id);
        return Response.success();
    }

    /**
     * 恢复任务
     */
    @Operation(summary = "恢复定时任务", description = "恢复已暂停的任务继续执行")
    @PostMapping("/{id}/resume")
    public Response<Void> resumeJob(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id
    ) {
        scheduleJobService.resumeJob(id);
        return Response.success();
    }

    /**
     * 禁用任务（长期停用，但保留配置）
     */
    @Operation(summary = "禁用定时任务", description = "长期停用任务，从Quartz调度器中移除，但保留配置信息。禁用后不可触发执行。")
    @PostMapping("/{id}/disable")
    public Response<Void> disableJob(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id
    ) {
        scheduleJobService.disableJob(id);
        return Response.success();
    }

    /**
     * 恢复启用禁用任务
     */
    @Operation(summary = "启用定时任务", description = "恢复被禁用（DISABLED）的定时任务，重新注册调度，状态变为ENABLED")
    @PostMapping("/{id}/enable")
    public Response<Void> enableJob(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id
    ) {
        scheduleJobService.enableJob(id);
        return Response.success();
    }

    /**
     * 立即触发任务
     */
    @Operation(summary = "立即触发任务", description = "不影响原有调度计划，立即执行一次任务")
    @PostMapping("/{id}/trigger")
    public Response<Void> triggerJob(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id
    ) {
        scheduleJobService.triggerJob(id);
        return Response.success();
    }

    /**
     * 更新Cron表达式
     */
    @Operation(summary = "更新Cron表达式", description = "动态更新任务的Cron调度表达式")
    @PutMapping("/{id}/cron")
    public Response<Void> updateCron(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "新的Cron表达式", required = true) @RequestParam String cronExpression
    ) {
        scheduleJobService.updateCron(id, cronExpression);
        return Response.success();
    }

    @Override
    protected void exportData(HttpServletResponse response, List<ScheduleJobDTO> data) {
        // TODO: 实现导出功能
    }
}

