package com.lanyuanxiaoyao.service.hudi.controller;

import com.lanyuanxiaoyao.service.configuration.entity.PageResponse;
import com.lanyuanxiaoyao.service.configuration.entity.hudi.HudiCleanerPlan;
import com.lanyuanxiaoyao.service.configuration.entity.hudi.HudiCompactionPlan;
import com.lanyuanxiaoyao.service.configuration.entity.hudi.HudiInstant;
import com.lanyuanxiaoyao.service.configuration.entity.hudi.HudiRollbackPlan;
import com.lanyuanxiaoyao.service.hudi.service.TimelineService;
import java.io.IOException;
import java.util.List;
import org.eclipse.collections.api.factory.Lists;
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 2023-05-01
 */
@RestController
@RequestMapping("timeline")
public class TimelineController {
    private static final Logger logger = LoggerFactory.getLogger(TimelineController.class);

    private final TimelineService timelineService;

    public TimelineController(TimelineService timelineService) {
        this.timelineService = timelineService;
    }

    @GetMapping("list")
    public PageResponse<HudiInstant> allInstants(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "count", defaultValue = "10") Integer count,
            @RequestParam(value = "order", required = false) String order,
            @RequestParam(value = "direction", required = false) String direction,
            @RequestParam("flink_job_id") Long flinkJobId,
            @RequestParam("alias") String alias,
            @RequestParam(value = "filter_type", required = false) List<String> filterType,
            @RequestParam(value = "filter_action", required = false) List<String> filterAction,
            @RequestParam(value = "filter_state", required = false) List<String> filterState
    ) throws IOException {
        return timelineService.timeline(
                page,
                count,
                order,
                direction,
                flinkJobId,
                alias,
                Lists.immutable.ofAll(filterType),
                Lists.immutable.ofAll(filterAction),
                Lists.immutable.ofAll(filterState)
        );
    }

    @GetMapping("list_hdfs")
    public PageResponse<HudiInstant> allInstants(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "count", defaultValue = "10") Integer count,
            @RequestParam(value = "order", required = false) String order,
            @RequestParam(value = "direction", required = false) String direction,
            @RequestParam("hdfs") String hdfs,
            @RequestParam(value = "filter_type", required = false) List<String> filterType,
            @RequestParam(value = "filter_action", required = false) List<String> filterAction,
            @RequestParam(value = "filter_state", required = false) List<String> filterState
    ) throws IOException {
        return timelineService.timeline(
                page,
                count,
                order,
                direction,
                hdfs,
                Lists.immutable.ofAll(filterType),
                Lists.immutable.ofAll(filterAction),
                Lists.immutable.ofAll(filterState)
        );
    }

    @GetMapping("read_compaction_plan")
    public HudiCompactionPlan readCompactionPlan(
            @RequestParam("flink_job_id") Long flinkJobId,
            @RequestParam("alias") String alias,
            @RequestParam("instant") String instant
    ) throws IOException {
        return timelineService.readCompactionPlan(flinkJobId, alias, instant);
    }

    @GetMapping("read_compaction_plan_hdfs")
    public HudiCompactionPlan readCompactionPlanHdfs(
            @RequestParam("hdfs") String hdfs,
            @RequestParam("instant") String instant
    ) throws IOException {
        return timelineService.readCompactionPlan(hdfs, instant);
    }

    @GetMapping("read_rollback_plan")
    public HudiRollbackPlan readRollbackPlan(
            @RequestParam("flink_job_id") Long flinkJobId,
            @RequestParam("alias") String alias,
            @RequestParam("instant") String instant
    ) throws IOException {
        return timelineService.readRollbackPlan(flinkJobId, alias, instant);
    }

    @GetMapping("read_rollback_plan_hdfs")
    public HudiRollbackPlan readRollbackPlanHdfs(
            @RequestParam("hdfs") String hdfs,
            @RequestParam("instant") String instant
    ) throws IOException {
        return timelineService.readRollbackPlan(hdfs, instant);
    }

    @GetMapping("read_cleaner_plan")
    public HudiCleanerPlan readCleanerPlan(
            @RequestParam("flink_job_id") Long flinkJobId,
            @RequestParam("alias") String alias,
            @RequestParam("instant") String instant
    ) throws IOException {
        return timelineService.readCleanerPlan(flinkJobId, alias, instant);
    }

    @GetMapping("read_cleaner_plan_hdfs")
    public HudiCleanerPlan readCleanerPlanHdfs(
            @RequestParam("hdfs") String hdfs,
            @RequestParam("instant") String instant
    ) throws IOException {
        return timelineService.readCleanerPlan(hdfs, instant);
    }

    @GetMapping("list_pending_compaction")
    public ImmutableList<HudiInstant> pendingCompactionInstants(
            @RequestParam("flink_job_id") Long flinkJobId,
            @RequestParam("alias") String alias
    ) throws IOException {
        return timelineService.pendingCompactionTimeline(flinkJobId, alias);
    }

    @GetMapping("exists_compaction_plan")
    public Boolean existsCompactionPlan(
            @RequestParam("flink_job_id") Long flinkJobId,
            @RequestParam("alias") String alias
    ) throws IOException {
        return !timelineService.pendingCompactionTimeline(flinkJobId, alias).isEmpty();
    }
}
