package com.yice.webadmin.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.github.pagehelper.page.PageMethod;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yice.common.core.annotation.MyRequestBody;
import com.yice.common.core.annotation.NoAuthInterface;
import com.yice.common.core.constant.ErrorCodeEnum;
import com.yice.common.core.object.*;
import com.yice.common.core.util.MyCommonUtil;
import com.yice.common.core.util.MyModelUtil;
import com.yice.common.core.util.MyPageUtil;
import com.yice.common.log.annotation.OperationLog;
import com.yice.common.log.model.constant.SysOperationLogType;
import com.yice.webadmin.app.config.PythonConfig;
import com.yice.webadmin.app.dto.ModelEstimateDto;
import com.yice.webadmin.app.dto.ModelTaskDto;
import com.yice.webadmin.app.model.*;
import com.yice.webadmin.app.service.*;
import com.yice.webadmin.app.util.FileUtil;
import com.yice.webadmin.app.vo.ModelEstimateVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 模型评估操作控制器类。
 *
 * @author linking
 * @date 2023-04-13
 */
@Api(tags = "模型评估管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/app/modelEstimate")
public class ModelEstimateController {

    @Autowired
    private ModelEstimateService modelEstimateService;
    @Autowired
    private ModelTaskService modelTaskService;
    @Autowired
    private ModelVersionService modelVersionService;
    @Autowired
    private DatasetVersionService datasetVersionService;
    @Autowired
    private TuningRunService tuningRunService;
    @Autowired
    private PythonConfig pythonConfig;


    /**
     * 新增模型评估数据，及其关联的从表数据。
     *
     * @param modelEstimateDto 新增主表对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"modelEstimateDto.taskId", "modelEstimateDto.searchString"})
    @OperationLog(type = SysOperationLogType.ADD)
    @PostMapping("/add")
    public ResponseResult<Long> add(
            @MyRequestBody ModelEstimateDto modelEstimateDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(modelEstimateDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        ModelEstimate modelEstimate = MyModelUtil.copyTo(modelEstimateDto, ModelEstimate.class);
        modelEstimate = modelEstimateService.saveNew(modelEstimate);
        return ResponseResult.success(modelEstimate.getTaskId());
    }

    /**
     * 获取预览命令。
     *
     * @param taskId 任务ID。
     * @return 应答结果对象。
     */
    @GetMapping("/getPreviewCommand")
    public ResponseResult<JSONArray> getPreviewCommand(@RequestParam Long taskId) {
        ModelEstimate modelEstimate = this.modelEstimateService.getById(taskId);
        ModelVersion modelVersion = this.modelVersionService.getById(modelEstimate.getModelVersionId());
        TuningRun tuningRun = this.tuningRunService.getById(modelVersion.getRunId());
        JSONObject jsonObject = (JSONObject) JSON.parse(modelEstimate.getConfiguration());
        JSONArray datasetVersionNames = new JSONArray();
        Set<Long> datasetVersionIds = Arrays.stream(modelEstimate.getDatasetVersionIds().split(",")).mapToLong(Long::parseLong).boxed().collect(Collectors.toSet());
        datasetVersionService.getInList(datasetVersionIds).stream()
                .map(DatasetVersion::getVersionName)
                .forEach(versionName -> {
                    try {
                        datasetVersionNames.add(versionName);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                });
        JSONArray array = new JSONArray();
        array.add("zh");
        array.add(modelVersion.getVersionName());
        array.add(modelVersion.getModelUrl());
        array.add(tuningRun.getTrainMethod());
        array.add(new JSONArray());
        array.add(jsonObject.get("quantizationLevel"));
        array.add(jsonObject.get("promptTemplate"));
        array.add("");
        array.add(false);
        array.add(false);
        array.add("none");
        array.add(pythonConfig.getDatasetFileMenu());
        array.add(datasetVersionNames);
        array.add(jsonObject.get("truncationLength"));
        array.add(jsonObject.get("maximumSampleSize"));
        array.add(jsonObject.get("batchSize"));
        array.add(true);
        array.add(jsonObject.get("maximumGeneratingLength"));
        array.add(jsonObject.get("ToppSamplingValue"));
        array.add(jsonObject.get("temperatureCoefficient"));
        array.add(this.pythonConfig.getModelEstimateFileBaseDir() + modelVersion.getVersionName() + File.separator + "evl_" + taskId);
        System.out.println(array);
        return ResponseResult.success(array);
    }

    /**
     * 提交任务状态信息。
     *
     * @return 应答结果对象，包含查询结果集。
     */
    @NoAuthInterface
    @OperationLog(type = SysOperationLogType.UPLOAD, saveResponse = false)
    @PostMapping("/postTaskStatus")
    public ResponseResult<Void> postTaskStatus(@MyRequestBody Long taskId,
                                               @MyRequestBody Long runTime,
                                               @MyRequestBody Integer taskStatus) {
        String errorMessage;
        ModelEstimate modelEstimate = this.modelEstimateService.getById(taskId);
        if (modelEstimate == null) {
            errorMessage = "数据验证失败，当前评估任务并不存在，请联系管理员！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        modelEstimate.setTaskStatus(taskStatus);
        modelEstimate.setUpdateTime(new Date());
        if (!modelEstimateService.updateStatusById(modelEstimate)) {
            errorMessage = "任务状态信息提交错误！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        return ResponseResult.success();
    }

    /**
     * 获取评估任务的日志。
     *
     * @param taskId 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @GetMapping("/getLog")
    public ResponseEntity<Resource> getLog(@RequestParam Long taskId) throws IOException {
        ModelEstimate modelEstimate = this.modelEstimateService.getById(taskId);
        ModelVersion modelVersion = this.modelVersionService.getById(modelEstimate.getModelVersionId());
        String url = this.pythonConfig.getModelEstimateFileBaseDir() + modelVersion.getVersionName() + File.separator + "evl_" + taskId + File.separator + "run_eval.log";
        return FileUtil.getFileByUrl(url);
    }

    /**
     * 获取任务状态。
     *
     * @param taskId 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @GetMapping("/getStatus")
    public ResponseResult<String> getStatus(@RequestParam Long taskId) throws IOException {
        ModelEstimate modelEstimate = this.modelEstimateService.getById(taskId);
        ModelVersion modelVersion = this.modelVersionService.getById(modelEstimate.getModelVersionId());
        String url = this.pythonConfig.getModelEstimateFileBaseDir() + modelVersion.getVersionName() + File.separator + "evl_" + taskId + File.separator + "all_results.json";
        File file = new File(url); // 指定文件路径
        ObjectMapper objectMapper = new ObjectMapper();
        return ResponseResult.success(objectMapper.readTree(file).toString());
    }

    /**
     * 获取任务状态明细。
     *
     * @param taskId 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @PostMapping(value = "/getStatusDetail")
    public ResponseResult<MyPageData<String>> getStatusDetail(@MyRequestBody Long taskId,
                                                              @MyRequestBody MyPageParam pageParam) throws IOException {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        ModelEstimate modelEstimate = this.modelEstimateService.getById(taskId);
        ModelVersion modelVersion = this.modelVersionService.getById(modelEstimate.getModelVersionId());
        Set<Long> datasetVersionIds = Arrays.stream(modelEstimate.getDatasetVersionIds().split(",")).mapToLong(Long::parseLong).boxed().collect(Collectors.toSet());
        List<DatasetVersion> datasetVersions = datasetVersionService.getInList(datasetVersionIds);
        List<String> dataList = new ArrayList<>();
        int i = 0;
        String url = this.pythonConfig.getModelEstimateFileBaseDir() + modelVersion.getVersionName() + File.separator + "evl_" + taskId + File.separator + "generated_predictions.jsonl";
        //获取评估输出详情
        List<JSONObject> jsonObjects = this.getFileJsonArray(url);
        for (DatasetVersion datasetVersion : datasetVersions) {
            ArrayNode arrayNode = (ArrayNode) new ObjectMapper().readTree(new File(datasetVersion.getFileUrl())); // 读取JSON文件内容并转换为ArrayNode对象
            for (JSONObject jsonNode : jsonObjects) { // 遍历JSON数组并取出每个元素（ObjectNode）中的数据
                jsonNode.put("datasetName",datasetVersion.getVersionName());
                jsonNode.put("input", arrayNode.get(i).get("instruction").textValue() + arrayNode.get(i).get("input").textValue());
                dataList.add(jsonNode.toJSONString());
                i++;
            }
        }
        int total = dataList.size(); // 获取总数据量
        int page = pageParam.getPageNum();
        int limit = pageParam.getPageSize();
        int startIndex = (page - 1) * limit; // 计算起始索引位置
        int endIndex = Math.min(page * limit, total); // 计算结束索引位置（不超出总数据量）
        List<String> pageDataList = dataList.subList(startIndex, endIndex); // 分页处理并取出指定页的数据列表
        return ResponseResult.success(MyPageUtil.makeResponseData(pageDataList, (long) total));
    }

    private List<JSONObject> getFileJsonArray(String url) {
        List<JSONObject> list = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(url))) {
            String line;
            while ((line = br.readLine()) != null) {
                JSONObject jsonObject = JSON.parseObject(line);
                list.add(jsonObject);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 修改模型评估数据，及其关联的从表数据。
     *
     * @param modelEstimateDto 修改后的对象。
     * @param modelTaskDto     一对一模型任务从表Dto。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"modelEstimateDto.taskId", "modelEstimateDto.searchString"})
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/update")
    public ResponseResult<Long> update(
            @MyRequestBody ModelEstimateDto modelEstimateDto,
            @MyRequestBody ModelTaskDto modelTaskDto) {
        String errorMessage;
        ResponseResult<Tuple2<ModelEstimate, JSONObject>> verifyResult =
                this.doBusinessDataVerifyAndConvert(modelEstimateDto, true, modelTaskDto);
        if (!verifyResult.isSuccess()) {
            return ResponseResult.errorFrom(verifyResult);
        }
        Tuple2<ModelEstimate, JSONObject> bizData = verifyResult.getData();
        ModelEstimate originalModelEstimate = bizData.getSecond().getObject("originalData", ModelEstimate.class);
        ModelEstimate modelEstimate = bizData.getFirst();
        if (!modelEstimateService.updateWithRelation(modelEstimate, originalModelEstimate, bizData.getSecond())) {
            errorMessage = "数据验证失败，[ModelEstimate] 数据不存在!";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        return ResponseResult.success(modelEstimate.getTaskId());
    }

    /**
     * 修改模型评估数据，及其关联的从表数据。
     *
     * @param modelEstimateDto 修改后的对象。
     * @return 应答结果对象，包含新增对象主键Id。
     */
    @ApiOperationSupport(ignoreParameters = {"modelEstimateDto.taskId", "modelEstimateDto.searchString"})
    @OperationLog(type = SysOperationLogType.UPDATE)
    @PostMapping("/updateById")
    public ResponseResult<Void> updateById(
            @MyRequestBody ModelEstimateDto modelEstimateDto) {
        String errorMessage = MyCommonUtil.getModelValidationError(modelEstimateDto, true);
        if (errorMessage != null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_VALIDATED_FAILED, errorMessage);
        }
        ModelEstimate modelEstimate = MyModelUtil.copyTo(modelEstimateDto, ModelEstimate.class);
        ModelEstimate originalModelEstimate = modelEstimateService.getById(modelEstimate.getTaskId());
        if (originalModelEstimate == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [数据] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        originalModelEstimate.setTaskStatus(modelEstimate.getTaskStatus());
        if (!modelEstimateService.updateById(originalModelEstimate)) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.success();
    }

    /**
     * 删除模型评估数据。
     *
     * @param taskId 删除对象主键Id。
     * @return 应答结果对象。
     */
    @OperationLog(type = SysOperationLogType.DELETE)
    @PostMapping("/delete")
    public ResponseResult<Void> delete(@MyRequestBody Long taskId) {
        if (MyCommonUtil.existBlankArgument(taskId)) {
            return ResponseResult.error(ErrorCodeEnum.ARGUMENT_NULL_EXIST);
        }
        return this.doDelete(taskId);
    }

    /**
     * 列出符合过滤条件的模型评估列表。
     *
     * @param modelEstimateDtoFilter 过滤对象。
     * @param modelTaskDtoFilter     一对一从表过滤对象。
     * @param orderParam             排序参数。
     * @param pageParam              分页参数。
     * @return 应答结果对象，包含查询结果集。
     */
    @PostMapping("/list")
    public ResponseResult<MyPageData<ModelEstimateVo>> list(
            @MyRequestBody ModelEstimateDto modelEstimateDtoFilter,
            @MyRequestBody ModelTaskDto modelTaskDtoFilter,
            @MyRequestBody MyOrderParam orderParam,
            @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        ModelEstimate modelEstimateFilter = MyModelUtil.copyTo(modelEstimateDtoFilter, ModelEstimate.class);
        ModelTask modelTaskFilter = MyModelUtil.copyTo(modelTaskDtoFilter, ModelTask.class);
        String orderBy = MyOrderParam.buildOrderBy(orderParam, ModelEstimate.class);
        List<ModelEstimate> modelEstimateList =
                modelEstimateService.getModelEstimateListWithRelation(modelEstimateFilter, modelTaskFilter, orderBy);
        return ResponseResult.success(MyPageUtil.makeResponseData(modelEstimateList, ModelEstimate.INSTANCE));
    }

    /**
     * 查看指定模型评估对象详情。
     *
     * @param taskId 指定对象主键Id。
     * @return 应答结果对象，包含对象详情。
     */
    @GetMapping("/view")
    public ResponseResult<ModelEstimateVo> view(@RequestParam Long taskId) {
        ModelEstimate modelEstimate = modelEstimateService.getByIdWithRelation(taskId, MyRelationParam.full());
        if (modelEstimate == null) {
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
        }
        ModelEstimateVo modelEstimateVo = ModelEstimate.INSTANCE.fromModel(modelEstimate);
        return ResponseResult.success(modelEstimateVo);
    }

    private ResponseResult<Tuple2<ModelEstimate, JSONObject>> doBusinessDataVerifyAndConvert(
            ModelEstimateDto modelEstimateDto,
            boolean forUpdate,
            ModelTaskDto modelTaskDto) {
        ErrorCodeEnum errorCode = ErrorCodeEnum.DATA_VALIDATED_FAILED;
        String errorMessage = MyCommonUtil.getModelValidationError(modelEstimateDto, false);
        if (errorMessage != null) {
            return ResponseResult.error(errorCode, errorMessage);
        }
        errorMessage = MyCommonUtil.getModelValidationError(modelTaskDto);
        if (errorMessage != null) {
            return ResponseResult.error(errorCode, "参数 [modelTaskDto] " + errorMessage);
        }
        // 全部关联从表数据的验证和转换
        JSONObject relationData = new JSONObject();
        CallResult verifyResult;
        // 下面是输入参数中，主表关联数据的验证。
        ModelEstimate modelEstimate = MyModelUtil.copyTo(modelEstimateDto, ModelEstimate.class);
        ModelEstimate originalData;
        if (forUpdate && modelEstimate != null) {
            originalData = modelEstimateService.getById(modelEstimate.getTaskId());
            if (originalData == null) {
                return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST);
            }
            relationData.put("originalData", originalData);
        }
        // 处理主表的一对一关联 [ModelTask]
        ModelTask modelTask = MyModelUtil.copyTo(modelTaskDto, ModelTask.class);
        verifyResult = modelTaskService.verifyRelatedData(modelTask);
        if (!verifyResult.isSuccess()) {
            return ResponseResult.errorFrom(verifyResult);
        }
        relationData.put("modelTask", modelTask);
        return ResponseResult.success(new Tuple2<>(modelEstimate, relationData));
    }

    private ResponseResult<Void> doDelete(Long taskId) {
        String errorMessage;
        // 验证关联Id的数据合法性
        ModelEstimate originalModelEstimate = modelEstimateService.getById(taskId);
        if (originalModelEstimate == null) {
            // NOTE: 修改下面方括号中的话述
            errorMessage = "数据验证失败，当前 [对象] 并不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        if (!modelEstimateService.remove(taskId)) {
            errorMessage = "数据操作失败，删除的对象不存在，请刷新后重试！";
            return ResponseResult.error(ErrorCodeEnum.DATA_NOT_EXIST, errorMessage);
        }
        return ResponseResult.success();
    }
}
