package com.ruoyi.platform.controller.model;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.service.ModelsService;
import com.ruoyi.platform.vo.ModelsVo;
import com.ruoyi.platform.vo.QueryModelMetricsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("newmodel")
@Api(value = "新模型管理")
public class NewModelFromGitController extends BaseController {

    @Resource
    private ModelsService modelsService;

    @PostMapping("/addModel")
    @ApiOperation("添加模型")
    public GenericsAjaxResult<String> addModelAndVersion(@RequestBody ModelsVo modelsVo) {
        return genericsSuccess(this.modelsService.newCreateModel(modelsVo));
    }

    @PostMapping("/addVersion")
    @ApiOperation("添加版本")
    public AjaxResult addVersion(@RequestBody ModelsVo modelsVo) throws Exception {
        return AjaxResult.success(this.modelsService.newCreateVersion(modelsVo));
    }

    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping("/upload")
    @ApiOperation(value = "上传模型", notes = "根据模型id上传模型文件，并将信息存入数据库。")
    public AjaxResult uploadModel(@RequestParam("file") MultipartFile[] files, @RequestParam("uuid") String uuid) throws Exception {
        return AjaxResult.success(this.modelsService.uploadModelLocal(files, uuid));
    }

    @GetMapping("/downloadAllFiles")
    @ApiOperation(value = "下载同一版本下所有模型，并打包")
    public ResponseEntity<InputStreamResource> downloadAllDatasetFiles(@RequestParam("name") String name, @RequestParam("identifier") String identifier, @RequestParam("id") Integer id, @RequestParam("version") String version, @RequestParam("is_public") Boolean isPublic) throws Exception {
        return modelsService.downloadAllModelFilesNew(name, identifier, id, version, isPublic);
    }

    @GetMapping("/downloadSingleFile")
    @ApiOperation(value = "下载单个模型文件", notes = "根据模型版本表id下载单个模型文件")
    public ResponseEntity<InputStreamResource> downloadDatasetlocal(@RequestParam("url") String url) throws Exception {
        return modelsService.downloadDatasetlocal(url);
    }

    @GetMapping("/queryModels")
    @ApiOperation("模型广场公开模型分页查询,根据model_type,model_tag筛选，true公开false私有")
    public AjaxResult queryModels(@RequestParam(value = "page") int page,
                                  @RequestParam(value = "size") int size,
                                  @RequestParam(value = "is_public", required = false) Boolean isPublic,
                                  @RequestParam(value = "model_type", required = false) String modelType,
                                  @RequestParam(value = "model_tag", required = false) String modelTag,
                                  @RequestParam(value = "name", required = false) String name) throws Exception {
        PageRequest pageRequest = PageRequest.of(page, size);
        ModelsVo modelsVo = new ModelsVo();
        modelsVo.setModelType(modelType);
        modelsVo.setModelTag(modelTag);
        modelsVo.setName(name);
        if (isPublic) {
            return AjaxResult.success(this.modelsService.newPubilcQueryByPage(modelsVo, pageRequest));
        } else {
            return AjaxResult.success(this.modelsService.newPersonalQueryByPage(modelsVo, pageRequest));
        }
    }

    @GetMapping("/queryVersions")
    @ApiOperation("分页查询模型版本")
    public AjaxResult queryVersions(@RequestParam(value = "page") int page,
                                    @RequestParam(value = "size") int size,
                                    @RequestParam("identifier") String identifier,
                                    @RequestParam("owner") String owner,
                                    @RequestParam("type") String type) throws Exception {
        PageRequest pageRequest = PageRequest.of(page, size);
        return AjaxResult.success(this.modelsService.queryVersions(pageRequest, identifier, owner, type));
    }

    @PostMapping("/queryVersionsMetrics")
    @ApiOperation("查询版本指标")
    public AjaxResult queryVersionsMetrics(@RequestBody QueryModelMetricsVo queryModelMetricsVo) throws Exception {
        return AjaxResult.success(this.modelsService.queryVersionsMetrics(queryModelMetricsVo));
    }


    @GetMapping("/getVersionList")
    @ApiOperation(value = "获取模型分支列表")
    public AjaxResult getVersionList(@RequestParam("identifier") String identifier, @RequestParam("owner") String owner) throws Exception {
        return AjaxResult.success(this.modelsService.getVersionList(identifier, owner));
    }


    @GetMapping("/getModelDetail")
    @ApiOperation(value = "获取模型详细信息")
    public AjaxResult getModelDetail(@RequestParam("id") Integer id, @RequestParam("identifier") String identifier, @RequestParam("owner") String owner, @RequestParam("version") String version, @RequestParam("is_public") Boolean isPublic) throws Exception {
        return AjaxResult.success(this.modelsService.getModelDetail(id, identifier, owner, version, isPublic));
    }

    @GetMapping("/getModelDependencyTree")
    @ApiOperation(value = "获取模型依赖关系树")
    public AjaxResult getModelDependencyTree(@RequestParam("id") Integer id, @RequestParam("identifier") String identifier, @RequestParam("version") String version) throws Exception {
        return AjaxResult.success(this.modelsService.getModelDependencyTree(id, identifier, version));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除模型")
    public AjaxResult deleteModel(@RequestParam("id") Integer id, @RequestParam("identifier") String identifier,
                                  @RequestParam("owner") String owner, @RequestParam("is_public") Boolean isPublic) throws Exception {
        this.modelsService.deleteModel(id, identifier, owner, isPublic);
        return AjaxResult.success();
    }

    @DeleteMapping("/deleteVersion")
    @ApiOperation(value = "删除模型版本")
    public AjaxResult deleteVersion(@RequestParam("id") Integer id, @RequestParam("identifier") String identifier, @RequestParam("owner") String owner, @RequestParam("version") String version
            , @RequestParam("relative_paths") String relativePaths) throws Exception {
        List<Map<String, Object>> versionList = this.modelsService.getVersionList(identifier, owner);
        if (versionList.size() == 1) {
            return AjaxResult.error("当前模型只有一个版本，不能删除该版本");
        }
        this.modelsService.deleteVersion(id, identifier, owner, version, relativePaths);
        return AjaxResult.success();
    }

    @PostMapping("/getVersionsCompare")
    @ApiOperation(value = "获取模型版本对比")
    public AjaxResult getVersionsCompare(@RequestBody QueryModelMetricsVo queryModelMetricsVo) throws Exception {
        return AjaxResult.success(this.modelsService.getVersionsCompare(queryModelMetricsVo));
    }

    @PostMapping("/praise/{id}")
    @ApiOperation(value = "点赞一个项目")
    public AjaxResult praise(@PathVariable("id") Integer id) throws Exception {
        return AjaxResult.success(this.modelsService.praise(id));
    }

    @DeleteMapping("/unpraise/{id}")
    @ApiOperation(value = "取消点赞一个项目")
    public AjaxResult unpraise(@PathVariable("id") Integer id) throws Exception {
        return AjaxResult.success(this.modelsService.unpraise(id));
    }
}
