use aide::axum::routing::{ApiMethodRouter, get_with};
use axum::Json;
use schemars::JsonSchema;
use serde::Serialize;

use crate::{
  backend::{
    BackendRouter,
    auth::{oidc::OidcState, settings::UserSettings},
  },
  db::{init::Connection, tables::ConnectionExt},
  error::Result,
};

pub fn router() -> BackendRouter {
  BackendRouter::new().api_route("/", auth_config_route())
}

pub fn auth_config_route() -> ApiMethodRouter<()> {
  get_with(config, |op| op.id("authConfig"))
}

#[derive(Serialize, Debug, JsonSchema)]
enum SSOType {
  Oidc,
  None,
}

#[derive(Serialize, JsonSchema)]
struct AuthConfig {
  sso_type: SSOType,
  instant_redirect: bool,
  #[cfg(feature = "mail")]
  mail_enabled: bool,
}

async fn config(
  oidc: OidcState,
  user: UserSettings,
  #[cfg(feature = "mail")] mailer: crate::mail::Mailer,
  db: Connection,
) -> Result<Json<AuthConfig>> {
  let sso_type = if oidc.is_enabled().await {
    SSOType::Oidc
  } else {
    SSOType::None
  };

  let user_settings = db.settings().get_settings::<UserSettings>().await?;
  #[cfg(feature = "mail")]
  let mail_enabled = mailer.is_active().await;

  let instant_redirect = user
    .sso_instant_redirect
    .unwrap_or(user_settings.sso_instant_redirect.unwrap_or(false));

  Ok(Json(AuthConfig {
    sso_type,
    instant_redirect,
    #[cfg(feature = "mail")]
    mail_enabled,
  }))
}
