// Code generated by TofuPilot SDK Generator. DO NOT EDIT.

use crate::client::TofuPilot;
use crate::error::Result;
use crate::types::*;

/// Client for the User API.
#[derive(Debug, Clone)]
pub struct UserClient<'a> {
    pub(crate) client: &'a TofuPilot,
}

impl<'a> UserClient<'a> {
    pub(crate) fn new(client: &'a TofuPilot) -> Self {
        Self { client }
    }

    /// List users
    ///
    /// Retrieve a list of users in your organization. Use the current parameter to get only the authenticated user profile and permissions.
    pub fn list(&self) -> ListBuilder<'a> {
        ListBuilder::new(self.client)
    }

}

// ---------------------------------------------------------------------------
// ListBuilder
// ---------------------------------------------------------------------------

/// Builder for [`UserClient::list`].
///
/// # Example
///
/// ```no_run
/// use tofupilot::TofuPilot;
///
/// # #[tokio::main]
/// # async fn main() -> tofupilot::Result<()> {
/// let client = TofuPilot::new("your-api-key");
/// let response = client.user().list()
///     .send()
///     .await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct ListBuilder<'a> {
    client: &'a TofuPilot,
    current: Option<bool>,
    server_url: Option<String>,
    timeout: Option<std::time::Duration>,
}

impl<'a> ListBuilder<'a> {
    pub(crate) fn new(client: &'a TofuPilot) -> Self {
        Self {
            client,
            current: None,
            server_url: None,
            timeout: None,
        }
    }

    /// Set the `current` query parameter.
    ///
    /// If true, returns only the current authenticated user
    pub fn current(mut self, value: impl Into<bool>) -> Self {
        self.current = Some(value.into());
        self
    }

    /// Override the server URL for this request.
    pub fn server_url(mut self, url: impl Into<String>) -> Self {
        self.server_url = Some(url.into());
        self
    }

    /// Override the timeout for this request.
    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Send the request.
    pub async fn send(self) -> Result<Vec<UserListResponse>> {

        let base_url = self.server_url
            .as_deref()
            .unwrap_or(&self.client.config.base_url);

        let url = format!("{}{}", base_url, "/v2/users");

        let mut request = self.client.http.request(
            reqwest::Method::GET,
            &url,
        );

        if let Some(timeout) = self.timeout {
            request = request.timeout(timeout);
        }

        if let Some(ref val) = self.current {
            request = request.query(&[("current", val.to_string())]);
        }


        let response = self.client.execute(
            request,
            "user-list",
            base_url,
        ).await?;
        let result: Vec<UserListResponse> = response.json().await?;
        Ok(result)
    }
}

