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

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.finetuning.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.finetuning.FineTuningDO;
import cn.iocoder.yudao.module.llm.service.finetuning.FineTuningService;

@Tag(name = "管理后台 - 模型微调")
@RestController
@RequestMapping("/llm/fine-tuning")
@Validated
public class FineTuningController {

    @Resource
    private FineTuningService fineTuningService;

    @PostMapping("/create")
    @Operation(summary = "创建模型微调")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning:create')")
    public CommonResult<Integer> createFineTuning(@Valid @RequestBody FineTuningSaveReqVO createReqVO) {
        return success(fineTuningService.createFineTuning(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模型微调")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning:update')")
    public CommonResult<Boolean> updateFineTuning(@Valid @RequestBody FineTuningSaveReqVO updateReqVO) {
        fineTuningService.updateFineTuning(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得模型微调")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning:query')")
    public CommonResult<FineTuningRespVO> getFineTuning(@RequestParam("id") Integer id) {
        FineTuningDO fineTuning = fineTuningService.getFineTuning(id);
        return success(BeanUtils.toBean(fineTuning, FineTuningRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模型微调分页")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning:query')")
    public CommonResult<PageResult<FineTuningRespVO>> getFineTuningPage(@Valid FineTuningPageReqVO pageReqVO) {
        PageResult<FineTuningDO> pageResult = fineTuningService.getFineTuningPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FineTuningRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模型微调 Excel")
//    @PreAuthorize("@ss.hasPermission('llm:fine-tuning:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFineTuningExcel(@Valid FineTuningPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FineTuningDO> list = fineTuningService.getFineTuningPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模型微调.xls", "数据", FineTuningRespVO.class,
                        BeanUtils.toBean(list, FineTuningRespVO.class));
    }

}