package com.purui.prims.headless.server.rest;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import com.purui.prims.headless.api.pojo.request.DataSetReq;
import com.purui.prims.headless.api.pojo.request.DatasetPersistenceConfigReq;
import com.purui.prims.headless.api.pojo.response.DataSetResp;
import com.purui.prims.headless.api.pojo.response.DatasetPersistenceConfigResp;
import com.purui.prims.headless.server.pojo.MetaFilter;
import com.purui.prims.headless.server.service.DataSetService;
import com.purui.prims.headless.server.service.DatasetPersistenceConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/semantic/dataSet")
@Api(tags = "指标数据集")
public class DataSetController {

    @Autowired
    private DataSetService dataSetService;

    @Autowired
    private DatasetPersistenceConfigService datasetPersistenceConfigService;

    @ApiOperation(value = "保存数据集")
    @PostMapping
    public DataSetResp save(@RequestBody DataSetReq dataSetReq,
                            HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return dataSetService.save(dataSetReq, user);
    }

    @ApiOperation(value = "更新数据集")
    @PutMapping
    public DataSetResp update(@RequestBody DataSetReq dataSetReq,
                              HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return dataSetService.update(dataSetReq, user);
    }

    @ApiOperation(value = "获取数据集详情")
    @GetMapping("/{id}")
    public DataSetResp getDataSet(@PathVariable("id") Long id) {
        return dataSetService.getDataSet(id);
    }

    @ApiOperation(value = "根据主题域获取数据集列表")
    @GetMapping("/getDataSetList")
    public List<DataSetResp> getDataSetList(@RequestParam("domainId") Long domainId, HttpServletRequest request,
                                            HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        MetaFilter metaFilter = new MetaFilter();
        metaFilter.setDomainId(domainId);
        return dataSetService.getDataSetList(metaFilter, user);
    }

    @ApiOperation(value = "获取所有数据集列表")
    @GetMapping("/getAllDataSetList")
    public List<DataSetResp> getAllDataSetList() {
        MetaFilter metaFilter = new MetaFilter();
        return dataSetService.getDataSetList(metaFilter);
    }

    @ApiOperation(value = "删除数据集")
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable("id") Long id,
                          HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        dataSetService.delete(id, user);
        return true;
    }

    @ApiOperation(value = "数据集持久化配置")
    @PostMapping("/persistenceConfig")
    public Boolean persistenceConfig(@RequestBody DatasetPersistenceConfigReq configReq) {
        return datasetPersistenceConfigService.save(configReq);
    }

    @ApiOperation(value = "获取数据集持久化配置")
    @GetMapping("/persistenceConfig/{datasetId}")
    public DatasetPersistenceConfigResp getPersistenceConfig(@PathVariable Long datasetId) {
        return datasetPersistenceConfigService.getByDatasetId(datasetId);
    }

}
