// @generated DO NOT EDIT.
//
// Generated from the CNB OpenAPI specification by `cnb-codegen`.
// Run `cargo run -p cnb-codegen --manifest-path codegen/Cargo.toml \
//      -- --spec codegen/spec/swagger.json --out src` to refresh.

#![allow(clippy::all)]
#![allow(missing_docs)]
#![allow(unused_imports)]
#![allow(unused_mut)]

use reqwest::Method;
use serde::Serialize;

use crate::error::{ApiError, Result};
use crate::http::HttpInner;
use crate::models;

// `KnowledgeBase` resource client (generated, 5 operations).

/// Resource client.
#[derive(Debug, Clone)]
pub struct KnowledgeBaseClient {
    inner: HttpInner,
}

impl KnowledgeBaseClient {
    /// Construct a new resource client. Normally obtained
    /// via [`crate::ApiClient`] rather than directly.
    pub fn new(inner: HttpInner) -> Self {
        Self { inner }
    }

    /// 删除知识库
    ///
    /// `DELETE /{repo}/-/knowledge/base`
    pub async fn delete_knowledge_base(&self, repo: String) -> Result<serde_json::Value> {
        let path = format!("/{}/-/knowledge/base", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::DELETE, url)
            .await
    }

    /// 获取知识库信息
    ///
    /// `GET /{repo}/-/knowledge/base`
    pub async fn get_knowledge_base_info(
        &self,
        repo: String,
    ) -> Result<crate::models::KnowledgeBaseInfoRes> {
        let path = format!("/{}/-/knowledge/base", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::KnowledgeBaseInfoRes>(Method::GET, url)
            .await
    }

    /// 获取当前支持的 Embedding 模型列表
    ///
    /// `GET /{repo}/-/knowledge/embedding/models`
    pub async fn get_models(&self, repo: String) -> Result<Vec<crate::models::EmbeddingModel>> {
        let path = format!("/{}/-/knowledge/embedding/models", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<Vec<crate::models::EmbeddingModel>>(Method::GET, url)
            .await
    }

    /// 查询知识库，使用文档：<https://docs.cnb.cool/zh/ai/knowledge-base.html>
    ///
    /// `POST /{repo}/-/knowledge/base/query`
    pub async fn query_knowledge_base(
        &self,
        repo: String,
        body: &crate::models::QueryKnowledgeBaseReq,
    ) -> Result<Vec<crate::models::QueryKnowledgeBaseRes>> {
        let path = format!("/{}/-/knowledge/base/query", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<Vec<crate::models::QueryKnowledgeBaseRes>, _>(
                Method::POST,
                url,
                body,
            )
            .await
    }

    /// 全局语义搜索 NPC 角色
    ///
    /// `GET /search/npc`
    pub async fn search_npc(
        &self,
        query: &SearchNpcQuery,
    ) -> Result<Vec<crate::models::SearchNpcRes>> {
        let path = "/search/npc".to_string();
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.key {
                pairs.append_pair("key", v);
            }
            if let Some(v) = query.top_n {
                pairs.append_pair("top_n", &v.to_string());
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::SearchNpcRes>>(Method::GET, url)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct SearchNpcQuery {
    /// 搜索关键词
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key: Option<String>,
    /// 返回结果数量，最大20
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub top_n: Option<i64>,
}

impl SearchNpcQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `key` query parameter.
    pub fn key(mut self, v: impl Into<String>) -> Self {
        self.key = Some(v.into());
        self
    }
    /// Set `top_n` query parameter.
    pub fn top_n(mut self, v: impl Into<i64>) -> Self {
        self.top_n = Some(v.into());
        self
    }
}
