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

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
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.*;
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.finetuningtask.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.finetuningtask.FineTuningTaskDO;
import cn.iocoder.yudao.module.llm.service.finetuningtask.FineTuningTaskService;

@Tag(name = "管理后台 - 模型调优 —— 微调任务")
@RestController
@RequestMapping("/llm/fine-tuning-task")
@Validated
public class FineTuningTaskController {

    @Resource
    private FineTuningTaskService fineTuningTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建模型调优 —— 微调任务")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:create')")
    public CommonResult<Long> createFineTuningTask(@Valid @RequestBody FineTuningTaskSaveReqVO createReqVO) {
        return success(fineTuningTaskService.createFineTuningTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模型调优 —— 微调任务")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:update')")
    public CommonResult<Boolean> updateFineTuningTask(@Valid @RequestBody FineTuningTaskSaveReqVO updateReqVO) {
        fineTuningTaskService.updateFineTuningTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模型调优 —— 微调任务")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:delete')")
    public CommonResult<Boolean> deleteFineTuningTask(@RequestParam("id") Long id) {
        fineTuningTaskService.deleteFineTuningTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模型调优 —— 微调任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:query')")
    public CommonResult<FineTuningTaskRespVO> getFineTuningTask(@RequestParam("id") Long id) {
        return success(fineTuningTaskService.getFineTuningTask(id));
    }

    @PutMapping("/stop")
    @Operation(summary = "停止模型调优 —— 微调任务")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:update')")
    public CommonResult<Boolean> stopFineTuningTask(@RequestParam("id") Long id) {
        fineTuningTaskService.stopFineTuningTask(id);
        return success(true);
    }

    @PutMapping("start")
    @Operation(summary = "部署模型调优 —— 微调任务")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:update')")
    public CommonResult<Boolean> startFineTuningTask(@RequestParam("id") Long id) {
        fineTuningTaskService.startFineTuningTask(id);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得模型调优 —— 微调任务分页")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:query')")
    public CommonResult<PageResult<FineTuningTaskRespVO>> getFineTuningTaskPage(@Valid FineTuningTaskPageReqVO pageReqVO) {
        PageResult<FineTuningTaskRespVO> pageResult = fineTuningTaskService.getFineTuningTaskPage1(pageReqVO);
        return success(pageResult);
    }
    @GetMapping("/all")
    @Operation(summary = "获得模型调优 —— 微调任务列表")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:query')")
    public CommonResult<List<FineTuningTaskRespVO>> getFineTuningTaskList() {
        List<FineTuningTaskRespVO> list = fineTuningTaskService.selectAll();
        return success(list);
    }

    @GetMapping("/enable")
    @Operation(summary = "获得模型调优 —— 获取已部署完成的调优任务")
    //    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:query')")
    public CommonResult<List<FineTuningTaskRespVO>> getEnableFineTuningTaskList() {
        List<FineTuningTaskRespVO> list = fineTuningTaskService.selectEnable();
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模型调优 —— 微调任务 Excel")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFineTuningTaskExcel(@Valid FineTuningTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FineTuningTaskDO> list = fineTuningTaskService.getFineTuningTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模型调优 —— 微调任务.xls", "数据", FineTuningTaskRespVO.class,
                        BeanUtils.toBean(list, FineTuningTaskRespVO.class));
    }
    @GetMapping("finetuning-log")
    @Operation(summary = "获得模型微调日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning:query')")
    public CommonResult<String> getFineTuningLog(@RequestParam("id") Long id) {
        String fineTuning = fineTuningTaskService.getFineTuningLog(id);
        return success(fineTuning);
    }

}
