package tech.easyflow.admin.controller.datacenter;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.row.Row;
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.DatacenterQueryRequest;
import tech.easyflow.datacenter.execution.model.DatacenterSchemaResponse;
import tech.easyflow.datacenter.execution.model.DatasetRef;
import tech.easyflow.datacenter.meta.model.DatacenterBatchRemoveRequest;
import tech.easyflow.datacenter.meta.model.DatacenterSaveDescriptionsRequest;
import tech.easyflow.datacenter.execution.service.DatacenterDatasetQueryService;
import tech.easyflow.datacenter.meta.service.DatacenterDatasetRegistryService;

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

@RestController
@RequestMapping("/api/v1/datacenterDataset")
public class DatacenterDatasetController {

    @Resource
    private DatacenterDatasetQueryService queryService;
    @Resource
    private DatacenterDatasetRegistryService registryService;

    @PostMapping("/queryPage")
    @SaCheckPermission("/api/v1/datacenterSource/query")
    public Result<Page<Row>> queryPage(@RequestBody DatacenterQueryRequest request) {
        return Result.ok(queryService.queryPage(request));
    }

    @GetMapping("/schema")
    @SaCheckPermission("/api/v1/datacenterSource/query")
    public Result<DatacenterSchemaResponse> schema(DatasetRef datasetRef) {
        return Result.ok(queryService.getSchema(datasetRef));
    }

    @GetMapping("/managedTables")
    @SaCheckPermission("/api/v1/datacenterSource/query")
    public Result<List<DatacenterTable>> managedTables(BigInteger sourceId, BigInteger catalogId) {
        return Result.ok(registryService.listManagedTables(sourceId, catalogId));
    }

    @PostMapping("/removeBatch")
    @SaCheckPermission("/api/v1/datacenterSource/save")
    public Result<Integer> removeBatch(@RequestBody DatacenterBatchRemoveRequest request) {
        return Result.ok(registryService.removeTables(request == null ? List.of() : request.getTableIds()));
    }

    @PostMapping("/saveDescriptions")
    @SaCheckPermission("/api/v1/datacenterSource/save")
    public Result<DatacenterSchemaResponse> saveDescriptions(@RequestBody DatacenterSaveDescriptionsRequest request) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        DatacenterTable table = registryService.saveDescriptions(
                request == null ? null : request.getTableId(),
                request == null ? null : request.getTableDesc(),
                request == null ? List.of() : request.getFields(),
                account
        );
        return Result.ok(queryService.getSchema(registryService.resolveDatasetRef(table.getId())));
    }
}
