package org.dromara.chat.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.chat.domain.bo.ChatModelBo;
import org.dromara.chat.domain.vo.ChatModelVo;
import org.dromara.chat.service.ChatModelService;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天模型控制器
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat/model")
public class ChatModelController extends BaseController {

    private final ChatModelService chatModelService;

    /**
     * 查询聊天模型列表
     */
    @SaCheckPermission("chat:model:list")
    @GetMapping("/list")
    public TableDataInfo<ChatModelVo> list(ChatModelBo bo, PageQuery pageQuery) {
        return chatModelService.queryPageList(bo, pageQuery);
    }

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

    /**
     * 获取聊天模型详细信息
     */
    @SaCheckPermission("chat:model:query")
    @GetMapping("/{id}")
    public R<ChatModelVo> getInfo(@PathVariable Long id) {
        return R.ok(chatModelService.queryById(id));
    }

    /**
     * 新增聊天模型
     */
    @SaCheckPermission("chat:model:add")
    @Log(title = "聊天模型", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ChatModelBo bo) {
        return toAjax(chatModelService.insertByBo(bo));
    }

    /**
     * 修改聊天模型
     */
    @SaCheckPermission("chat:model:edit")
    @Log(title = "聊天模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ChatModelBo bo) {
        return toAjax(chatModelService.updateByBo(bo));
    }

    /**
     * 删除聊天模型
     */
    @SaCheckPermission("chat:model:remove")
    @Log(title = "聊天模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(chatModelService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据模型分类查询模型列表
     */
    @SaCheckPermission("chat:model:query")
    @GetMapping("/category/{category}")
    public R<List<ChatModelVo>> getByCategory(@PathVariable String category) {
        return R.ok(chatModelService.queryListByCategory(category));
    }

    /**
     * 根据模型名称查询模型
     */
    @SaCheckPermission("chat:model:query")
    @GetMapping("/name/{modelName}")
    public R<ChatModelVo> getByModelName(@PathVariable String modelName) {
        return R.ok(chatModelService.queryByModelName(modelName));
    }

    /**
     * 查询显示的模型列表
     */
    @SaCheckPermission("chat:model:query")
    @GetMapping("/show")
    public R<List<ChatModelVo>> getShowModelList() {
        return R.ok(chatModelService.queryShowModelList());
    }

    /**
     * 根据模型类型查询模型列表
     */
    @SaCheckPermission("chat:model:query")
    @GetMapping("/type/{modelType}")
    public R<List<ChatModelVo>> getByModelType(@PathVariable String modelType) {
        return R.ok(chatModelService.queryListByModelType(modelType));
    }

}