package com.lanyuanxiaoyao.service.info.controller;

import cn.hutool.core.util.ObjectUtil;
import com.eshore.odcp.hudi.connector.entity.FlinkJob;
import com.eshore.odcp.hudi.connector.entity.SyncState;
import com.eshore.odcp.hudi.connector.entity.TableMeta;
import com.lanyuanxiaoyao.service.configuration.entity.PageResponse;
import com.lanyuanxiaoyao.service.configuration.entity.info.JobAndMetas;
import com.lanyuanxiaoyao.service.configuration.entity.info.JobIdAndAlias;
import com.lanyuanxiaoyao.service.info.service.InfoService;
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-04-24
 */
@RestController
@RequestMapping("info")
public class InfoController {
    private static final Logger logger = LoggerFactory.getLogger(InfoController.class);

    private final InfoService infoService;

    public InfoController(InfoService infoService) {
        this.infoService = infoService;
    }

    @GetMapping("/job_id_alias")
    public PageResponse<JobIdAndAlias> jobIdAndAlias(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "count", defaultValue = "10") Integer count,
            @RequestParam(value = "flink_job_id", required = false) Long flinkJobId,
            @RequestParam(value = "alias", required = false) String alias,
            @RequestParam(value = "order", required = false) String order,
            @RequestParam(value = "direction", required = false) String direction,
            @RequestParam(value = "filter_run_mode", required = false) List<String> selectedRunMode,
            @RequestParam(value = "filter_compaction_status", required = false) List<String> selectedCompactionStatus
    ) {
        return infoService.findAllJobIdAndAlias(
                page,
                count,
                flinkJobId,
                alias,
                order,
                direction,
                Lists.immutable.ofAll(selectedRunMode),
                Lists.immutable.ofAll(selectedCompactionStatus)
        );
    }

    @GetMapping("/job_metas")
    public ImmutableList<JobAndMetas> jobAndMetas() {
        return infoService.jobAndMetas();
    }

    @GetMapping("/flink_job/list")
    public ImmutableList<FlinkJob> flinkJobs() {
        return infoService.flinkJobs();
    }

    @GetMapping("/flink_job/detail")
    public FlinkJob flinkJob(@RequestParam("flink_job_id") Long flinkJobId) {
        return infoService.flinkJob(flinkJobId);
    }

    @GetMapping("/table_meta/list")
    public ImmutableList<TableMeta> tableMetas(@RequestParam(value = "flink_job_id", required = false) Long flinkJobId) {
        return ObjectUtil.isNull(flinkJobId)
                ? infoService.tableMetas()
                : infoService.tableMetas(flinkJobId);
    }

    @GetMapping("/table_meta/detail")
    public TableMeta tableMeta(@RequestParam("flink_job_id") Long flinkJobId, @RequestParam("alias") String alias) {
        return infoService.tableMeta(flinkJobId, alias);
    }

    @GetMapping("/sync_state/detail")
    public SyncState syncState(@RequestParam("flink_job_id") Long flinkJobId, @RequestParam("alias") String alias) {
        return infoService.syncState(flinkJobId, alias);
    }
}
