pub mod oauth_repository;
pub mod user_repository;

use std::str::FromStr;

use anyhow::Context;
use sqlx::{postgres::PgConnectOptions, PgPool, Pool};

#[derive(Debug, Clone)]
pub struct Postgres {
    pool: Pool<sqlx::Postgres>,
}

impl Postgres {
    pub async fn new(url: &str) -> Result<Self, anyhow::Error> {
        let pool = PgPool::connect_with(
            PgConnectOptions::from_str(url)
                .with_context(|| format!("Invalid database url: {}", url))?,
        )
        .await
        .with_context(|| format!("Failed to open database at {}", url))?;

        Ok(Self { pool })
    }

    pub fn pool(&self) -> Pool<sqlx::Postgres> {
        self.pool.clone()
    }
}
