package tech.easyflow.job.service.impl;

import org.springframework.stereotype.Component;
import tech.easyflow.ai.service.WorkflowScheduleReferenceProvider;
import tech.easyflow.ai.vo.OfflineImpactBindingVo;
import tech.easyflow.job.entity.SysJob;
import tech.easyflow.job.service.SysJobService;

import java.math.BigInteger;
import java.util.List;

/**
 * 定时任务对工作流的引用查询实现。
 */
@Component
public class SysJobWorkflowReferenceProvider implements WorkflowScheduleReferenceProvider {

    private final SysJobService sysJobService;

    /**
     * 创建定时任务工作流引用查询提供者。
     *
     * @param sysJobService 定时任务服务
     */
    public SysJobWorkflowReferenceProvider(SysJobService sysJobService) {
        this.sysJobService = sysJobService;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<OfflineImpactBindingVo> listScheduledJobsByWorkflowId(BigInteger workflowId) {
        return sysJobService.listWorkflowJobsByWorkflowId(workflowId).stream()
                .map(this::toBinding)
                .toList();
    }

    /**
     * 将定时任务转换为删除影响摘要。
     *
     * @param job 定时任务
     * @return 影响摘要
     */
    private OfflineImpactBindingVo toBinding(SysJob job) {
        OfflineImpactBindingVo binding = new OfflineImpactBindingVo();
        binding.setId(job.getId());
        binding.setTitle(job.getJobName());
        return binding;
    }
}
