package org.dromara.system.controller.ai;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AiModelVo;
import org.dromara.system.domain.bo.AiModelBo;
import org.dromara.system.service.IAiModelService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 模型
 *
 * @author wangkang
 * @date 2025-09-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/model")
public class AiModelController extends BaseController {

    private final IAiModelService aiModelService;

    /**
     * 查询模型列表
     */
    @SaCheckPermission("system:model:list")
    @GetMapping("/list")
    public TableDataInfo<AiModelVo> list(AiModelBo bo, PageQuery pageQuery) {
        return aiModelService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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