package com.lanyuanxiaoyao.service.command.commands;

import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.service.command.utils.CommandLineUtils;
import com.lanyuanxiaoyao.service.command.utils.TableUtils;
import com.lanyuanxiaoyao.service.common.Constants;
import com.lanyuanxiaoyao.service.common.entity.TableMeta;
import com.lanyuanxiaoyao.service.forest.service.InfoService;
import com.lanyuanxiaoyao.service.forest.service.ScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;

/**
 * 压缩相关操作
 *
 * @author lanyuanxiaoyao
 * @date 2024-03-19
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@ShellComponent("压缩任务相关操作")
public class CompactionCommand extends AbstractUtilShellComponent {
    private static final Logger logger = LoggerFactory.getLogger(CompactionCommand.class);
    private final static String RUN_CONFIRMATION_MESSAGE = "操作将会提交压缩任务, 请再次确认操作";

    private final InfoService infoService;
    private final ScheduleService scheduleService;

    public CompactionCommand(InfoService infoService, ScheduleService scheduleService) {
        this.infoService = infoService;
        this.scheduleService = scheduleService;
    }

    @ShellMethod("启动表压缩任务")
    public String compactionRun(
            @ShellOption(help = "Flink job id") Long flinkJobIds,
            @ShellOption(help = "别名") String alias,
            @ShellOption(
                    help = "Ignore double check",
                    defaultValue = "false"
            ) Boolean ignoreCheck
    ) {
        TableMeta meta = infoService.tableMetaDetail(flinkJobIds, alias);
        return CommandLineUtils.generateResultLines(
                () -> {
                    if (ObjectUtil.isEmpty(meta)) {
                        return "没有找到指定的表信息";
                    } else {
                        System.out.println(TableUtils.makeTableMeta(meta));
                        if (doubleCheck(RUN_CONFIRMATION_MESSAGE, ignoreCheck)) {
                            scheduleService.scheduleTable(meta.getJob().getId(), meta.getAlias());
                            return Constants.OPERATION_DONE;
                        } else {
                            return Constants.OPERATION_CANCEL;
                        }
                    }
                }
        );
    }
}
