use sqlx::query_as;
use uuid::Uuid;

use crate::client::AppDbClient;

#[derive(Clone, Debug)]
pub struct User {
    pub id: Uuid,
    pub uid: String,
    pub email: String,
}

impl User {
    pub fn with_id_in<I: IntoIterator<Item = Uuid>>(ids: I) -> WithIdInQuery {
        let ids: Vec<Uuid> = ids.into_iter().collect();
        WithIdInQuery { ids }
    }
}

#[derive(Clone, Debug)]
pub struct WithIdInQuery {
    ids: Vec<Uuid>,
}

impl WithIdInQuery {
    pub async fn fetch_all(self, app_db: &mut AppDbClient) -> Result<Vec<User>, sqlx::Error> {
        query_as!(
            User,
            "
select * from users
where id = any($1)
",
            self.ids.as_slice()
        )
        .fetch_all(&mut *app_db.conn)
        .await
    }
}
