package com.lanyuanxiaoyao.service.executor.manager.controller;

import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.executor.manager.service.ExecutorTaskService;
import java.io.IOException;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lanyuanxiaoyao
 * @date 2024-01-10
 */
@RestController
@RequestMapping("task")
public class ExecutorTaskController {
    private static final Logger logger = LoggerFactory.getLogger(ExecutorTaskController.class);

    private final ExecutorTaskService executorTaskService;

    public ExecutorTaskController(ExecutorTaskService executorTaskService) {
        this.executorTaskService = executorTaskService;
    }

    @GetMapping("scan")
    public String scan(
            @RequestParam("key") String key,
            @RequestParam(value = "hdfs", required = false) String hdfs,
            @RequestParam(value = "pulsar", required = false) String pulsar,
            @RequestParam(value = "pulsar_topic", required = false) String pulsarTopic,
            @RequestParam(value = "scan_source", defaultValue = "false") Boolean scanSource,
            @RequestParam(value = "scan_queue", defaultValue = "false") Boolean scanQueue,
            @RequestParam(value = "scan_log", defaultValue = "false") Boolean scanLog,
            @RequestParam(value = "scan_base", defaultValue = "false") Boolean scanBase,
            @RequestParam(value = "scan_target", defaultValue = "false") Boolean scanTarget
    ) throws Exception {
        logger.info("Enter method: scan[key, hdfs, pulsar, pulsarTopic, scanSource, scanQueue, scanLog, scanBase, scanTarget]. " + "key:" + key + "," + "hdfs:" + hdfs + "," + "pulsar:" + pulsar + "," + "pulsarTopic:" + pulsarTopic + "," + "scanSource:" + scanSource + "," + "scanQueue:" + scanQueue + "," + "scanLog:" + scanLog + "," + "scanBase:" + scanBase + "," + "scanTarget:" + scanTarget);
        if (!scanSource && !scanQueue && !scanLog && !scanBase && !scanTarget) {
            throw new RuntimeException("Must choose one mode");
        }
        if (scanQueue && (StrUtil.isBlank(pulsar) || StrUtil.isBlank(pulsar))) {
            throw new RuntimeException("Pulsar topic or url cannot be empty");
        }
        if ((scanLog || scanBase) && StrUtil.isBlank(hdfs)) {
            throw new RuntimeException("Hdfs path cannot be empty");
        }
        return executorTaskService.scanAvro(key, hdfs, pulsar, pulsarTopic, scanSource, scanQueue, scanLog, scanBase, scanTarget);
    }

    @GetMapping("latest_op_ts")
    public String latestOpTs(@RequestParam("hdfs") String hdfs) throws Exception {
        if (StrUtil.isBlank(hdfs)) {
            throw new RuntimeException("Hdfs path cannot be empty");
        }
        return executorTaskService.scanLatestOpTs(hdfs);
    }

    @GetMapping("results")
    public ImmutableList<String> results(
            @RequestParam("task_id") String taskId,
            @RequestParam(value = "limit", defaultValue = "1000") Integer limit
    ) throws IOException {
        return executorTaskService.taskResult(taskId, limit);
    }
}
