package com.lanyuanxiaoyao.service.info.controller;

import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.service.common.entity.TableMeta;
import com.lanyuanxiaoyao.service.configuration.entity.info.SimpleTableMeta;
import com.lanyuanxiaoyao.service.configuration.entity.info.TableMetaAdd;
import com.lanyuanxiaoyao.service.info.service.TableMetaService;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * Table Meta
 *
 * @author lanyuanxiaoyao
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/info")
public class TableMetaController {
    private static final Logger logger = LoggerFactory.getLogger(TableMetaController.class);

    private final TableMetaService tableMetaService;

    public TableMetaController(TableMetaService tableMetaService) {
        this.tableMetaService = tableMetaService;
    }

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

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

    @GetMapping("/simple_table_metas")
    public ImmutableList<SimpleTableMeta> simpleList(
            @RequestParam(value = "flink_job_id", required = false) Long flinkJobId,
            @RequestParam(value = "alias", required = false) String alias
    ) {
        return tableMetaService.simpleTableMetas(flinkJobId, alias);
    }

    @GetMapping("/exists_table")
    public Boolean existsTable(@RequestParam("flink_job_id") Long flinkJobId, @RequestParam("alias") String alias) {
        return tableMetaService.existsTable(flinkJobId, alias);
    }

    @GetMapping("/non_exists_table")
    public Boolean nonExistsTable(@RequestParam("flink_job_id") Long flinkJobId, @RequestParam("alias") String alias) {
        return !tableMetaService.existsTable(flinkJobId, alias);
    }

    @GetMapping("/exists_table_by_hdfs")
    public Boolean existsTableByHdfs(@RequestParam("hdfs") String hdfs) {
        return tableMetaService.existsTableByHdfs(hdfs);
    }

    @GetMapping("/non_exists_table_by_hdfs")
    public Boolean nonExistsTableByHdfs(@RequestParam("hdfs") String hdfs) {
        return !tableMetaService.existsTableByHdfs(hdfs);
    }

    @GetMapping("/table_count")
    public Long tableCount() {
        return tableMetaService.tableCount();
    }

    @GetMapping("/table_focus_count")
    public Long tableFocusCount() {
        return tableMetaService.tableFocusCount();
    }

    @GetMapping("/hudi_count")
    public Long hudiCount() {
        return tableMetaService.hudiCount();
    }

    @GetMapping("/hudi_focus_count")
    public Long hudiFocusCount() {
        return tableMetaService.hudiFocusCount();
    }

    @GetMapping("/hive_count")
    public Long hiveCount() {
        return tableMetaService.hiveCount();
    }

    @GetMapping("/hive_focus_count")
    public Long hiveFocusCount() {
        return tableMetaService.hiveFocusCount();
    }

    @GetMapping("/disable_table")
    public void disableTable(@RequestParam("flink_job_id") Long flinkJobId, @RequestParam("alias") String alias) {
        tableMetaService.disableTable(flinkJobId, alias);
    }

    @PostMapping("/table_meta/save")
    public void saveTableMeta(@RequestBody TableMetaAdd addon) {
        tableMetaService.saveTableMeta(addon);
    }
}
