use crate::doqium_auth_client::apis::admin_api;
use crate::doqium_auth_client::models::AdminListUsers200ResponseInner;
use anyhow::Result;

use super::DoqiumClient;

pub struct AuthRepository<'a> {
    client: &'a DoqiumClient,
}

impl<'a> AuthRepository<'a> {
    pub fn new(client: &'a DoqiumClient) -> Self {
        Self { client }
    }

    pub async fn list_users(&self) -> Result<Vec<AdminListUsers200ResponseInner>> {
        let users = admin_api::admin_list_users(self.client.auth_config()).await?;
        Ok(users)
    }

    pub async fn block_user(&self, user_id: &str, reason: &str) -> Result<()> {
        let body = crate::doqium_auth_client::models::AdminBlockUserRequest {
            reason: reason.to_string(),
        };
        admin_api::admin_block_user(self.client.auth_config(), user_id, body).await?;
        Ok(())
    }

    pub async fn unblock_user(&self, user_id: &str) -> Result<()> {
        admin_api::admin_unblock_user(self.client.auth_config(), user_id).await?;
        Ok(())
    }

    pub async fn logout_user(&self, user_id: &str) -> Result<u32> {
        let response = admin_api::admin_logout_user(self.client.auth_config(), user_id).await?;
        Ok(response.revoked_tokens.unwrap_or(0) as u32)
    }

    pub async fn delete_user(&self, user_id: &str) -> Result<()> {
        crate::doqium_auth_client::apis::admin_api::admin_delete_user(
            self.client.auth_config(),
            user_id,
        )
        .await?;
        Ok(())
    }

    pub async fn get_stats(&self) -> Result<serde_json::Value> {
        let stats =
            crate::doqium_auth_client::apis::admin_api::admin_get_stats(self.client.auth_config())
                .await?;
        Ok(stats)
    }
}
