use crate::AppService;
use crate::error::AppError;
use db::sqlx;
use model::ai::AiModelModelTagModel;
use session::Session;

impl AppService {
    pub async fn ai_model_tags(
        &self,
        ctx: &Session,
        model_id: uuid::Uuid,
    ) -> Result<Vec<String>, AppError> {
        let _user_uid = self.ai_require_login(ctx).await?;
        self.ai_tag_list_inner(model_id).await
    }

    pub async fn ai_tag_list_inner(
        &self,
        model_id: uuid::Uuid,
    ) -> Result<Vec<String>, AppError> {
        let tags = sqlx::query_as::<_, AiModelModelTagModel>(
            "SELECT model, tag, created_at FROM ai_model_model_tag WHERE model = $1 ORDER BY tag ASC",
        )
        .bind(model_id)
        .fetch_all(self.db.reader())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        Ok(tags.into_iter().map(|t| t.tag).collect())
    }
}
