package org.jk.rag.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.jk.common.excel.utils.ExcelUtil;
import org.jk.rag.domain.bo.ModelConfigBo;
import org.jk.rag.domain.dto.ConfigReqDTO;
import org.jk.rag.domain.vo.ModelConfigVo;
import org.jk.rag.service.IModelConfigService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.jk.common.idempotent.annotation.RepeatSubmit;
import org.jk.common.log.annotation.Log;
import org.jk.common.web.core.BaseController;
import org.jk.common.mybatis.core.page.PageQuery;
import org.jk.common.core.domain.R;
import org.jk.common.core.validate.AddGroup;
import org.jk.common.core.validate.EditGroup;
import org.jk.common.log.enums.BusinessType;
import org.jk.common.mybatis.core.page.TableDataInfo;

/**
 * 模型配置
 *
 * @author Lion Li
 * @date 2026-01-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/modelConfig")
public class ModelConfigController extends BaseController {

    private final IModelConfigService modelConfigService;

    /**
     * 查询模型配置列表
     */
    @SaCheckPermission("system:modelConfig:list")
    @GetMapping("/list")
    public TableDataInfo<ModelConfigVo> list(ModelConfigBo bo, PageQuery pageQuery) {
        return modelConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出模型配置列表
     */
    @SaCheckPermission("system:modelConfig:export")
    @Log(title = "模型配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ModelConfigBo bo, HttpServletResponse response) {
        List<ModelConfigVo> list = modelConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "模型配置", ModelConfigVo.class, response);
    }

    /**
     * 获取模型配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:modelConfig:query")
    @GetMapping("/{id}")
    public R<ModelConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(modelConfigService.queryById(id));
    }

    /**
     * 新增模型配置
     */
    @SaCheckPermission("system:modelConfig:add")
    @Log(title = "模型配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ModelConfigBo bo) {
        return toAjax(modelConfigService.insertByBo(bo));
    }

    /**
     * 修改模型配置
     */
    @SaCheckPermission("system:modelConfig:edit")
    @Log(title = "模型配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ModelConfigBo bo) {
        return toAjax(modelConfigService.updateByBo(bo));
    }

    /**
     * 删除模型配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:modelConfig:remove")
    @Log(title = "模型配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(modelConfigService.deleteWithValidByIds(List.of(ids), true));
    }

    @GetMapping("/rerankList")
    public R<List<ModelConfigVo>> rerankList(ModelConfigBo bo) {
        return R.ok(modelConfigService.queryRerankList(bo));
    }

    @GetMapping("/modelList")
    public R<List<ModelConfigVo>> modelList(ModelConfigBo bo) {
        return R.ok(modelConfigService.queryModelList(bo));
    }

    @Log(title = "模型配置", businessType = BusinessType.DELETE)
    @PutMapping("/updateConfigStatus/{id}")
    public R<Void> updateConfigStatus(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] id) {
        return toAjax(modelConfigService.updateStatus(id[0]));
    }

    @GetMapping("/configList")
    public R<ConfigReqDTO> configList() {
        return R.ok(modelConfigService.queryConfig());
    }

    @PostMapping("/editSysConfig")
    public R<Void> editSysConfig(@RequestBody ModelConfigBo bo) {
        modelConfigService.editSysConfig(bo);
        return R.ok();
    }


}
