/// Keycloak OpenID Connect settings.
#[derive(Debug)]
pub struct KeycloakConfig {
    pub url: String,
    pub realm: String,
    pub client_id: String,
    pub redirect_uri: String,
    pub app_url: String,
}

impl KeycloakConfig {
    pub fn from_env() -> Option<Self> {
        let url = std::env::var("KEYCLOAK_URL").ok()?;
        let realm = std::env::var("KEYCLOAK_REALM").ok()?;
        let client_id = std::env::var("KEYCLOAK_CLIENT_ID").ok()?;
        let redirect_uri = std::env::var("REDIRECT_URI").ok()?;
        let app_url = std::env::var("APP_URL").ok()?;
        Some(Self {
            url,
            realm,
            client_id,
            redirect_uri,
            app_url,
        })
    }

    pub fn auth_endpoint(&self) -> String {
        format!(
            "{}/realms/{}/protocol/openid-connect/auth",
            self.url, self.realm
        )
    }

    pub fn token_endpoint(&self) -> String {
        format!(
            "{}/realms/{}/protocol/openid-connect/token",
            self.url, self.realm
        )
    }

    pub fn userinfo_endpoint(&self) -> String {
        format!(
            "{}/realms/{}/protocol/openid-connect/userinfo",
            self.url, self.realm
        )
    }

    pub fn logout_endpoint(&self) -> String {
        format!(
            "{}/realms/{}/protocol/openid-connect/logout",
            self.url, self.realm
        )
    }
}
