use crate::AppService;
use crate::ai::types::AiModelCardResponse;
use crate::error::AppError;
use db::sqlx;
use model::ai::AiModelCardModel;
use session::Session;

impl AppService {
    pub async fn ai_model_card(
        &self,
        ctx: &Session,
        model_id: uuid::Uuid,
    ) -> Result<Option<AiModelCardResponse>, AppError> {
        let _user_uid = self.ai_require_login(ctx).await?;
        self.ai_card_get_inner(model_id).await
    }

    pub async fn ai_card_get_inner(
        &self,
        model_id: uuid::Uuid,
    ) -> Result<Option<AiModelCardResponse>, AppError> {
        let card = sqlx::query_as::<_, AiModelCardModel>(
            "SELECT model, overview, strengths, limitations, safety_notes, eval_summary, metadata, created_at, updated_at \
             FROM ai_model_card WHERE model = $1",
        )
        .bind(model_id)
        .fetch_optional(self.db.reader())
        .await
        .map_err(|e| AppError::DatabaseError(e.to_string()))?;

        Ok(card.map(AiModelCardResponse::from))
    }
}
