package tech.easyflow.admin.controller.datacenter;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.bind.annotation.*;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.entity.LoginAccount;
import tech.easyflow.common.satoken.util.SaTokenUtil;
import tech.easyflow.datacenter.entity.DatacenterTable;
import tech.easyflow.datacenter.execution.model.DatacenterConnectionTestResult;
import tech.easyflow.datacenter.meta.entity.DatacenterSource;
import tech.easyflow.datacenter.meta.model.DatacenterBatchRegisterRequest;
import tech.easyflow.datacenter.meta.model.DatacenterCatalogMeta;
import tech.easyflow.datacenter.meta.model.DatacenterRemoveSourceRequest;
import tech.easyflow.datacenter.meta.model.DatacenterTableDetailMeta;
import tech.easyflow.datacenter.meta.service.DatacenterSourceService;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("/api/v1/datacenterSource")
public class DatacenterSourceController {

    @Resource
    private DatacenterSourceService sourceService;

    @PostMapping("/testConnection")
    @SaCheckPermission("/api/v1/datacenterSource/query")
    public Result<DatacenterConnectionTestResult> testConnection(@RequestBody DatacenterSource source) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(sourceService.testConnection(source, account));
    }

    @PostMapping("/save")
    @SaCheckPermission("/api/v1/datacenterSource/save")
    public Result<DatacenterSource> save(@RequestBody DatacenterSource source) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(sourceService.saveSource(source, account));
    }

    @GetMapping("/page")
    @SaCheckPermission("/api/v1/datacenterSource/query")
    public Result<Page<DatacenterSource>> page(Long pageNumber, Long pageSize) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(sourceService.pageSources(pageNumber, pageSize, account));
    }

    @GetMapping("/catalogs")
    @SaCheckPermission("/api/v1/datacenterSource/query")
    public Result<List<DatacenterCatalogMeta>> catalogs(BigInteger sourceId) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(sourceService.listCatalogs(sourceId, account));
    }

    @GetMapping("/tables")
    @SaCheckPermission("/api/v1/datacenterSource/query")
    public Result<List<DatacenterTable>> tables(BigInteger sourceId, String catalogName) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(sourceService.listTables(sourceId, catalogName, account));
    }

    @GetMapping("/tableDetail")
    @SaCheckPermission("/api/v1/datacenterSource/query")
    public Result<DatacenterTableDetailMeta> tableDetail(BigInteger sourceId, String catalogName, String tableName,
                                                         @RequestParam(defaultValue = "false") boolean register) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(sourceService.getTableDetail(sourceId, catalogName, tableName, register, account));
    }

    @PostMapping("/registerBatch")
    @SaCheckPermission("/api/v1/datacenterSource/save")
    public Result<List<DatacenterTable>> registerBatch(@RequestBody DatacenterBatchRegisterRequest request) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(sourceService.batchRegisterTables(request, account));
    }

    @PostMapping("/remove")
    @SaCheckPermission("/api/v1/datacenterSource/save")
    public Result<Void> remove(@RequestBody DatacenterRemoveSourceRequest request) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        sourceService.removeSource(request == null ? null : request.getSourceId(), account);
        return Result.ok();
    }
}
