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

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.AuthType;
import com.purui.prims.headless.api.pojo.ModelInfo;
import com.purui.prims.headless.api.pojo.request.FieldRemovedReq;
import com.purui.prims.headless.api.pojo.request.MetaBatchReq;
import com.purui.prims.headless.api.pojo.request.ModelReq;
import com.purui.prims.headless.api.pojo.response.DatabaseResp;
import com.purui.prims.headless.api.pojo.response.ModelResp;
import com.purui.prims.headless.api.pojo.response.UnAvailableItemResp;
import com.purui.prims.headless.server.pojo.ModelFilter;
import com.purui.prims.headless.server.service.ModelService;
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.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 模型管理API
 *
 * @author charlie
 * @date 2024/7/5
 */
@RestController
@RequestMapping("/api/semantic/model")
@Api(tags = "模型管理")
public class ModelController {

    private ModelService modelService;

    public ModelController(ModelService modelService) {
        this.modelService = modelService;
    }

    @ApiOperation(value = "创建模型")
    @PostMapping("/createModel")
    public Boolean createModel(@RequestBody ModelReq modelReq,
                               HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        modelService.createModel(modelReq, user);
        return true;
    }

    @ApiOperation(value = "更新模型")
    @PutMapping("/updateModel")
    public Boolean updateModel(@RequestBody ModelReq modelReq,
                               HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        modelService.updateModel(modelReq, user);
        return true;
    }

    @ApiOperation(value = "删除模型")
    @DeleteMapping("/deleteModel/{modelId}")
    public Boolean deleteModel(@PathVariable("modelId") Long modelId,
                               HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        modelService.deleteModel(modelId, user);
        return true;
    }

    @ApiOperation(value = "获取主题域下的所有模型列表")
    @GetMapping("/getModelList/{domainId}")
    public List<ModelResp> getModelList(@PathVariable("domainId") Long domainId,
                                        HttpServletRequest request, HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return modelService.getModelListWithAuth(user, domainId, AuthType.ADMIN);
    }

    @ApiOperation(value = "获取模型详情")
    @GetMapping("/getModel/{id}")
    public ModelResp getModel(@PathVariable("id") Long id) {
        return modelService.getModel(id);
    }

    @ApiOperation(value = "根据多个模型ID获取模型详情列表")
    @GetMapping("/getModelListByIds/{modelIds}")
    public List<ModelResp> getModelListByIds(@PathVariable("modelIds") String modelIds) {
        List<Long> ids = Arrays.stream(modelIds.split(",")).map(Long::parseLong).collect(Collectors.toList());
        ModelFilter modelFilter = new ModelFilter();
        modelFilter.setIds(ids);
        List<ModelResp> modelResps = modelService.getModelList(modelFilter);
        // 计算相关指标数量
        modelService.calcMetricCount(modelResps);
        return modelResps;
    }

    @ApiOperation(value = "获取主题域中的所有模型")
    @GetMapping("/getAllModels/{domainId}")
    public List<ModelResp> getAllModelByDomainId(@PathVariable("domainId") Long domainId) {
        return modelService.getAllModelByDomainIds(Lists.newArrayList(domainId));
    }

    @GetMapping("/getModelDatabase/{modelId}")
    public DatabaseResp getModelDatabase(@PathVariable("modelId") Long modelId) {
        return modelService.getDatabaseByModelId(modelId);
    }

    @ApiOperation(value = "获取模型数据")
    @GetMapping("/getModelInfo/{modelId}")
    public ModelInfo getModelInfo(@PathVariable("modelId") Long modelId) {
        return modelService.getModelInfo(modelId);
    }

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

    @PostMapping("/getUnAvailableItem")
    public UnAvailableItemResp getUnAvailableItem(@RequestBody FieldRemovedReq fieldRemovedReq) {
        return modelService.getUnAvailableItem(fieldRemovedReq);
    }

}
