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


import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import com.purui.prims.common.pojo.enums.SensitiveLevelEnum;
import com.purui.prims.dscheduler.model.TaskInstance;
import com.purui.prims.dscheduler.model.TaskLog;
import com.purui.prims.headless.api.pojo.DrillDownDimension;
import com.purui.prims.headless.api.pojo.MetricQueryDefaultConfig;
import com.purui.prims.headless.api.pojo.request.MetaBatchReq;
import com.purui.prims.headless.api.pojo.request.MetricPersistenceConfigReq;
import com.purui.prims.headless.api.pojo.request.MetricReq;
import com.purui.prims.headless.api.pojo.request.PageMetricReq;
import com.purui.prims.headless.api.pojo.response.MetricPersistenceConfigResp;
import com.purui.prims.headless.api.pojo.response.MetricResp;
import com.purui.prims.headless.server.pojo.MetaFilter;
import com.purui.prims.headless.server.pojo.MetricFilter;
import com.purui.prims.headless.server.pojo.MetricLineage;
import com.purui.prims.headless.server.pojo.MetricVersion;
import com.purui.prims.headless.server.service.MetricLineageService;
import com.purui.prims.headless.server.service.MetricPersistenceConfigService;
import com.purui.prims.headless.server.service.MetricService;
import com.purui.prims.headless.server.service.MetricVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.RestController;

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

/**
 * 指标管理服务类
 *
 * @author charlie
 * @since 2024-07-25
 */
@RestController
@RequestMapping("/api/semantic/metric")
@Api(tags = "指标管理")
public class MetricController {

    private MetricService metricService;
    private MetricVersionService metricVersionService;
    private MetricLineageService metricLineageService;
    private MetricPersistenceConfigService metricPersistenceConfigService;

    public MetricController(MetricService metricService, MetricLineageService metricLineageService,
                            MetricVersionService metricVersionService, MetricPersistenceConfigService metricPersistenceConfigService) {
        this.metricService = metricService;
        this.metricLineageService = metricLineageService;
        this.metricVersionService = metricVersionService;
        this.metricPersistenceConfigService = metricPersistenceConfigService;
    }

    @ApiOperation(value = "创建指标")
    @PostMapping("/createMetric")
    public MetricResp createMetric(@RequestBody MetricReq metricReq,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        return metricService.createMetric(metricReq, user);
    }

    @ApiOperation(value = "更新指标")
    @PutMapping("/updateMetric")
    public MetricResp updateMetric(@RequestBody MetricReq metricReq,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        return metricService.updateMetric(metricReq, user);
    }

    @ApiOperation(value = "批量更新指标状态")
    @PostMapping("/batchUpdateStatus")
    public Boolean batchUpdateStatus(@RequestBody MetaBatchReq metaBatchReq,
                                     HttpServletRequest request,
                                     HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        metricService.batchUpdateStatus(metaBatchReq, user);
        return true;
    }

    @ApiOperation(value = "批量发布指标")
    @PostMapping("/batchPublish")
    public Boolean batchPublish(@RequestBody MetaBatchReq metaBatchReq,
                                HttpServletRequest request,
                                HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        metricService.batchPublish(metaBatchReq.getIds(), user);
        return true;
    }

    @ApiOperation(value = "批量取消发布指标")
    @PostMapping("/batchUnPublish")
    public Boolean batchUnPublish(@RequestBody MetaBatchReq metaBatchReq,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        metricService.batchUnPublish(metaBatchReq.getIds(), user);
        return true;
    }

    @PostMapping("/batchUpdateClassifications")
    public Boolean batchUpdateClassifications(@RequestBody MetaBatchReq metaBatchReq,
                                              HttpServletRequest request,
                                              HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        metricService.batchUpdateClassifications(metaBatchReq, user);
        return true;
    }

    @PostMapping("/batchUpdateSensitiveLevel")
    public Boolean batchUpdateSensitiveLevel(@RequestBody MetaBatchReq metaBatchReq,
                                             HttpServletRequest request,
                                             HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        metricService.batchUpdateSensitiveLevel(metaBatchReq, user);
        return true;
    }

    @ApiOperation(value = "获取模型下的所有指标")
    @GetMapping("/getMetricList/{modelId}")
    public List<MetricResp> getMetricList(@PathVariable("modelId") Long modelId) {
        MetaFilter metaFilter = new MetaFilter(Lists.newArrayList(modelId));
        return metricService.getMetrics(metaFilter);
    }

    @GetMapping("/getMetricsToCreateNewMetric/{modelId}")
    public List<MetricResp> getMetricsToCreateNewMetric(@PathVariable("modelId") Long modelId) {
        return metricService.getMetricsToCreateNewMetric(modelId);
    }

    @ApiOperation(value = "查询指标")
    @PostMapping("/queryMetric")
    public PageInfo<MetricResp> queryMetric(@RequestBody PageMetricReq pageMetricReq,
                                            HttpServletRequest request,
                                            HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return metricService.queryMetricMarket(pageMetricReq, user);
    }

    @Deprecated
    @GetMapping("getMetric/{modelId}/{bizName}")
    public MetricResp getMetric(@PathVariable("modelId") Long modelId,
                                @PathVariable("bizName") String bizName) {
        return metricService.getMetric(modelId, bizName);
    }

    @ApiOperation(value = "获取指标详情")
    @GetMapping("getMetric/{id}")
    public MetricResp getMetric(@PathVariable("id") Long id,
                                HttpServletRequest request,
                                HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return metricService.getMetric(id, user);
    }

    @ApiOperation(value = "删除指标")
    @DeleteMapping("deleteMetric/{id}")
    public Boolean deleteMetric(@PathVariable("id") Long id,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        metricService.deleteMetric(id, user);
        return true;
    }

    @GetMapping("/getAllHighSensitiveMetric")
    public List<MetricResp> getAllHighSensitiveMetric() {
        MetricFilter metricFilter = new MetricFilter();
        metricFilter.setSensitiveLevel(SensitiveLevelEnum.HIGH.getCode());
        return metricService.getMetrics(metricFilter);
    }

    @ApiOperation(value = "获取指标维度")
    @GetMapping("/getDrillDownDimension")
    public List<DrillDownDimension> getDrillDownDimension(Long metricId) {
        return metricService.getDrillDownDimension(metricId);
    }

    @ApiOperation(value = "保存指标默认查询配置")
    @PostMapping("/saveMetricQueryDefaultConfig")
    public boolean saveMetricQueryDefaultConfig(@RequestBody MetricQueryDefaultConfig queryDefaultConfig,
                                                HttpServletRequest request,
                                                HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        metricService.saveMetricQueryDefaultConfig(queryDefaultConfig, user);
        return true;
    }

    @ApiOperation(value = "获取指标默认查询配置")
    @GetMapping("getMetricQueryDefaultConfig/{metricId}")
    public MetricQueryDefaultConfig getMetricQueryDefaultConfig(@PathVariable("metricId") Long metricId,
                                                                HttpServletRequest request,
                                                                HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return metricService.getMetricQueryDefaultConfig(metricId, user);
    }

    @ApiOperation(value = "获取指标版本记录")
    @GetMapping("/version/{metricId}")
    public List<MetricVersion> queryMetricVersion(@PathVariable Long metricId) {
        return metricVersionService.queryMetricVersion(metricId);
    }

    @ApiOperation(value = "获取指标血缘")
    @GetMapping("/getMetricLineage/{metricId}")
    public MetricLineage getMetricLineage(@PathVariable Long metricId) {
        return metricLineageService.getLineages(metricId);
    }

    @ApiOperation(value = "查询指标的调度记录")
    @GetMapping("/queryTask/{metricId}")
    public List<TaskInstance> queryTaskInstances(@PathVariable("metricId") Long metricId, String startDate, String endDate) {
        return metricService.queryTaskInstances(metricId, startDate, endDate);
    }

    @ApiOperation(value = "获取调度详细日志")
    @GetMapping("/queryTaskLog/{metricId}/{instanceId}")
    public TaskLog queryTaskLog(@PathVariable("metricId") Long metricId, @PathVariable("instanceId") int instanceId) {
        return metricService.queryTaskLog(metricId, instanceId);
    }

    @ApiOperation(value = "立即调度")
    @PostMapping("/runTask/{metricId}")
    public Boolean runTask(@PathVariable Long metricId) {
        return metricService.runTask(metricId);
    }

    @ApiOperation(value = "指标数据持久化配置")
    @PostMapping("/persistenceConfig")
    public Boolean persistenceConfig(@RequestBody MetricPersistenceConfigReq configReq) {
        return metricPersistenceConfigService.save(configReq);
    }

    @ApiOperation(value = "获取指标数据持久化配置")
    @GetMapping("/persistenceConfig/{metricId}")
    public MetricPersistenceConfigResp getPersistenceConfig(@PathVariable Long metricId) {
        return metricPersistenceConfigService.getByMetricId(metricId);
    }
}
