use db::database::AppDatabase;
use models::rooms::room_ai;
use models::rooms::room_message::{Column as RmCol, Entity as RoomMessage};
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, QueryOrder, QuerySelect};
use uuid::Uuid;

use crate::error::RoomError;

pub async fn get_room_history(
    db: &AppDatabase,
    room_id: Uuid,
    limit: usize,
) -> Result<Vec<models::rooms::room_message::Model>, RoomError> {
    let messages = RoomMessage::find()
        .filter(RmCol::Room.eq(room_id))
        .order_by_desc(RmCol::Seq)
        .limit(limit as u64)
        .all(db)
        .await?;

    Ok(messages)
}

pub async fn get_user_names(
    db: &AppDatabase,
    user_ids: &[Uuid],
) -> std::collections::HashMap<Uuid, String> {
    use models::users::User;

    let mut names = std::collections::HashMap::new();
    if user_ids.is_empty() {
        return names;
    }

    let users = User::find()
        .filter(models::users::user::Column::Uid.is_in(user_ids.to_vec()))
        .all(db)
        .await
        .unwrap_or_default();

    for user in users {
        names.insert(user.uid, user.username);
    }

    names
}

pub async fn get_room_ai_config(
    db: &AppDatabase,
    room_id: Uuid,
) -> Result<Option<room_ai::Model>, RoomError> {
    let ai_config = room_ai::Entity::find()
        .filter(room_ai::Column::Room.eq(room_id))
        .one(db)
        .await?;

    Ok(ai_config)
}

pub async fn extract_mention_context(_content: &str) -> Vec<agent::chat::Mention> {
    Vec::new()
}
