use crate::api::AdminUser;
use crate::apis::auth_api;
use anyhow::Result;

pub async fn admin_list_users(token: &str) -> Result<Vec<AdminUser>> {
    auth_api::admin_list_users(token).await
}

pub async fn admin_find_user(token: &str, selector: &str) -> Result<AdminUser> {
    let users = admin_list_users(token).await?;

    users
        .into_iter()
        .find(|u| {
            u.id == selector
                || u.username.to_lowercase() == selector.to_lowercase()
                || u.email.to_lowercase() == selector.to_lowercase()
                || u.name.to_lowercase() == selector.to_lowercase()
        })
        .ok_or_else(|| anyhow::anyhow!("User not found: {}", selector))
}

pub async fn admin_delete_user(token: &str, user_id: &str) -> Result<()> {
    auth_api::admin_delete_user(token, user_id).await
}

pub async fn admin_block_user(token: &str, user_id: &str, reason: &str) -> Result<()> {
    auth_api::admin_block_user(token, user_id, reason).await
}

pub async fn admin_unblock_user(token: &str, user_id: &str) -> Result<()> {
    auth_api::admin_unblock_user(token, user_id).await
}

pub async fn admin_logout_user(token: &str, user_id: &str) -> Result<u32> {
    auth_api::admin_logout_user(token, user_id).await
}

pub async fn admin_auth_stats(token: &str) -> Result<serde_json::Value> {
    auth_api::admin_auth_stats(token).await
}
