package com.lanyuanxiaoyao.service.info.controller;

import cn.hutool.core.util.StrUtil;
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.configuration.entity.info.TableInfoSearchCache;
import com.lanyuanxiaoyao.service.info.service.InfoService;
import com.lanyuanxiaoyao.service.info.service.TableMetaService;
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;
    private final TableMetaService tableMetaService;

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

    @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_hudi_table_type", required = false) List<String> selectHudiTableType,
            @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(selectHudiTableType),
                Lists.immutable.ofAll(selectedRunMode),
                Lists.immutable.ofAll(selectedCompactionStatus)
        );
    }

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

    @GetMapping("/all_flink_job_id_and_alias")
    public ImmutableList<JobIdAndAlias> allFlinkJobIdAndAlias() {
        return tableMetaService.allTableInfoSearchCache()
                .collect(cache -> new JobIdAndAlias(cache.getFlinkJobId(), cache.getAlias()))
                .distinct();
    }

    @GetMapping("/all_flink_job_id")
    public ImmutableList<Long> allFlinkJobId(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "alias", required = false) String alias
    ) {
        return tableMetaService.allTableInfoSearchCache()
                .select(cache -> StrUtil.isBlank(key) || StrUtil.contains(cache.getFlinkJobId().toString(), key))
                .select(cache -> StrUtil.isBlank(alias) || StrUtil.contains(cache.getAlias(), alias))
                .collect(TableInfoSearchCache::getFlinkJobId)
                .distinct();
    }

    @GetMapping("/all_alias")
    public ImmutableList<String> allAlias(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "flink_job_id", required = false) String flinkJobId
    ) {
        return tableMetaService.allTableInfoSearchCache()
                .select(cache -> StrUtil.isBlank(key) || StrUtil.contains(cache.getAlias(), key))
                .select(cache -> StrUtil.isBlank(flinkJobId) || StrUtil.contains(cache.getFlinkJobId().toString(), flinkJobId))
                .collect(TableInfoSearchCache::getAlias)
                .distinct();
    }

    @GetMapping("/all_hdfs")
    public ImmutableList<String> allHdfs(@RequestParam(value = "key", required = false) String key) {
        return tableMetaService.allTableInfoSearchCache()
                .select(cache -> StrUtil.isBlank(key) || StrUtil.contains(cache.getAlias(), key) || StrUtil.contains(cache.getHdfs(), key))
                .collect(TableInfoSearchCache::getHdfs)
                .distinct();
    }

    @GetMapping("/all_pulsar")
    public ImmutableList<String> allPulsar(@RequestParam(value = "key", required = false) String key) {
        return tableMetaService.allTableInfoSearchCache()
                .select(cache -> StrUtil.isBlank(key) || StrUtil.contains(cache.getAlias(), key) || StrUtil.contains(cache.getPulsar(), key))
                .collect(TableInfoSearchCache::getPulsar)
                .distinct();
    }

    @GetMapping("/all_pulsar_topic")
    public ImmutableList<String> allPulsarTopic(@RequestParam(value = "key", required = false) String key) {
        return tableMetaService.allTableInfoSearchCache()
                .select(cache -> StrUtil.isBlank(key) || StrUtil.contains(cache.getAlias(), key) || StrUtil.contains(cache.getTopic(), key))
                .collect(TableInfoSearchCache::getTopic)
                .distinct();
    }
}
