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

import com.alibaba.fastjson.JSONObject;
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.basemodel.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.basemodel.BaseModelDO;
import cn.iocoder.yudao.module.llm.service.basemodel.BaseModelService;

@Tag(name = "管理后台 - 基座模型")
@RestController
@RequestMapping("/llm/base-model")
@Validated
public class BaseModelController {

    @Resource
    private BaseModelService baseModelService;

    @PostMapping("/create")
    @Operation(summary = "创建基座模型")
    @PreAuthorize("@ss.hasPermission('llm:base-model:create')")
    public CommonResult<Integer> createBaseModel(@Valid @RequestBody BaseModelSaveReqVO createReqVO) {
        return success(baseModelService.createBaseModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新基座模型")
    @PreAuthorize("@ss.hasPermission('llm:base-model:update')")
    public CommonResult<Boolean> updateBaseModel(@Valid @RequestBody BaseModelSaveReqVO updateReqVO) {
        baseModelService.updateBaseModel(updateReqVO);
        return success(true);
    }

    @PutMapping("/active")
    @Operation(summary = "启动")
    @PreAuthorize("@ss.hasPermission('llm:base-model:update')")
    public CommonResult<Boolean> active(@Valid @RequestBody BaseModelSaveReqVO updateReqVO) {
        updateReqVO.setIsActive(true);
        baseModelService.updateBaseModel(updateReqVO);
        return success(true);
    }

    @PutMapping("/unactive")
    @Operation(summary = "禁用")
    @PreAuthorize("@ss.hasPermission('llm:base-model:update')")
    public CommonResult<Boolean> unactive(@Valid @RequestBody BaseModelSaveReqVO updateReqVO) {
        updateReqVO.setIsActive(false);
        baseModelService.updateBaseModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除基座模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:base-model:delete')")
    public CommonResult<Boolean> deleteBaseModel(@RequestParam("id") Integer id) {
        baseModelService.deleteBaseModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得基座模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:base-model:query')")
    public CommonResult<BaseModelRespVO> getBaseModel(@RequestParam("id") Long id) {
        BaseModelDO baseModel = baseModelService.getBaseModel(id);
        return success(BeanUtils.toBean(baseModel, BaseModelRespVO.class));
    }

    @GetMapping("/getParamModel")
    @Operation(summary = "获取参数模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:base-model:query')")
    public CommonResult<JSONObject> getParamModel(@RequestParam("id") Long id) {
        BaseModelDO baseModel = baseModelService.getBaseModel(id);
        //todo 获取参数模板api
        return success(new JSONObject());
    }

    @GetMapping("/page")
    @Operation(summary = "获得基座模型分页")
    @PreAuthorize("@ss.hasPermission('llm:base-model:query')")
    public CommonResult<PageResult<BaseModelRespVO>> getBaseModelPage(@Valid BaseModelPageReqVO pageReqVO) {
        PageResult<BaseModelDO> pageResult = baseModelService.getBaseModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseModelRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得基座模型列表，下拉菜单")
    @PreAuthorize("@ss.hasPermission('llm:base-model:query')")
    public CommonResult<List<BaseModelRespVO>> getBaseModelList() {
        List<BaseModelDO> pageResult = baseModelService.getBaseModelList();
        return success(BeanUtils.toBean(pageResult, BaseModelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出基座模型 Excel")
    @PreAuthorize("@ss.hasPermission('llm:base-model:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseModelExcel(@Valid BaseModelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseModelDO> list = baseModelService.getBaseModelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "基座模型.xls", "数据", BaseModelRespVO.class,
                        BeanUtils.toBean(list, BaseModelRespVO.class));
    }

}
