package cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanual;

import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetQuestionPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetQuestionRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanual.manualAnnoVo.ManualModelAnswerPageReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskmanual.ModelAssessTaskDimensionDO;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassesstaskmanual.ModelAssessTaskDimensionMapper;
import cn.iocoder.yudao.module.llm.service.dataset.DatasetQuestionService;
import cn.iocoder.yudao.module.llm.service.modelassesstaskmanual.ManualModelAnswerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanual.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskmanual.ModelAssessTaskManualDO;
import cn.iocoder.yudao.module.llm.service.modelassesstaskmanual.ModelAssessTaskManualService;

@Tag(name = "管理后台 - 模型评估任务 —— 人工评估")
@RestController
@RequestMapping("/llm/model-assess-task-manual")
@Validated
public class ModelAssessTaskManualController {

    @Resource
    private ModelAssessTaskManualService modelAssessTaskManualService;
    @Resource
    ModelAssessTaskDimensionMapper modelAssessTaskDimensionMapper;
    @Resource
    private ManualModelAnswerService manualModelAnswerService;

    @PostMapping("/create")
    @Operation(summary = "创建模型评估任务 —— 人工评估")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:create')")
    public CommonResult<Long> createModelAssessTaskManual(@Valid @RequestBody ModelAssessTaskManualSaveReqVO createReqVO) {
        return success(modelAssessTaskManualService.createModelAssessTaskManual(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模型评估任务 —— 人工评估")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:update')")
    public CommonResult<Boolean> updateModelAssessTaskManual(@Valid @RequestBody ModelAssessTaskManualSaveReqVO updateReqVO) {
        modelAssessTaskManualService.updateModelAssessTaskManual(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模型评估任务 —— 人工评估")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:delete')")
    public CommonResult<Boolean> deleteModelAssessTaskManual(@RequestParam("id") Long id) {
        modelAssessTaskManualService.deleteModelAssessTaskManual(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模型评估任务 —— 人工评估")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:query')")
    public CommonResult<ModelAssessTaskManualRespVO> getModelAssessTaskManual(@RequestParam("id") Long id) {
        ModelAssessTaskManualRespVO modelAssessTaskManual = modelAssessTaskManualService.getModelAssessTaskManual(id);
        return success(modelAssessTaskManual);
    }

    @GetMapping("/page")
    @Operation(summary = "获得模型评估任务 —— 人工评估分页")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:query')")
    public CommonResult<PageResult<ModelAssessTaskManualRespVO>> getModelAssessTaskManualPage(@Valid ModelAssessTaskManualPageReqVO pageReqVO) {
        PageResult<ModelAssessTaskManualRespVO> pageResult = modelAssessTaskManualService.getModelAssessTaskManualPage1(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模型评估任务 —— 人工评估 Excel")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportModelAssessTaskManualExcel(@Valid ModelAssessTaskManualPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ModelAssessTaskManualDO> list = modelAssessTaskManualService.getModelAssessTaskManualPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模型评估任务 —— 人工评估.xls", "数据", ModelAssessTaskManualRespVO.class,
                        BeanUtils.toBean(list, ModelAssessTaskManualRespVO.class));
    }

    @GetMapping("/gitReport")
    @Operation(summary = "查看报告详情")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:query')")
    public CommonResult<List<ManualEvaluationReportRespVO>> gitViewTheReportDetails(@RequestParam("id") Long id) {
        //TODO 人工评估-查看报告待补充
        List<ManualEvaluationReportRespVO> manualEvaluationReportRespVOS = modelAssessTaskManualService.gitViewTheReportDetails(id);
        return success(manualEvaluationReportRespVOS);
    }

    @GetMapping("/getManualAssessment")
    @Operation(summary = "获取模型结果")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:query')")
    public CommonResult<PageResult<ManualAssessmentPageRespVO>> getManualAssessment(@Valid ManualModelAnswerPageReqVO pageReqVO) {
        return success(manualModelAnswerService.getManualAssessment(pageReqVO));
    }

    @GetMapping("/gitManualReport")
    @Operation(summary = "获取模型结果")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:query')")
    public CommonResult<List<String>> queryTheEvaluationType(@Valid ManualModelAnswerPageReqVO pageReqVO) {
        List<String> evaluationTypeList = manualModelAnswerService.queryTheEvaluationType(pageReqVO);
        return success(evaluationTypeList);
    }



    @PostMapping("/backup")
    @Operation(summary = "备份")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:update')")
    public CommonResult<Boolean> backedUp(@RequestParam("id") Long id) {
        modelAssessTaskManualService.backupModelAssessTaskManual(id);
        return success(true);
    }

    @PostMapping("/unBackup")
    @Operation(summary = "取消备份")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:update')")
    public CommonResult<Boolean> unBackup(@RequestParam("id") Long id) {
        modelAssessTaskManualService.unBackup(id);
        return success(true);
    }
    @PostMapping("/saveTheLabelData")
    @Operation(summary = "保存标注数据")
//    @PreAuthorize("@ss.hasPermission('llm:model-assess-task-manual:create')")
    public void saveTheLabelData(ModelAssessTaskManualSaveReqVO updateReqV){
        //TODO 人工评估-保存标注数据待补充
    }

}
