//! Runique built-in admin resources: rights and groups.
//!
//! Automatically injected into the admin registry without declaration in `admin!{}`.

mod droit;
mod groupe;
mod user;

use crate::admin::helper::resource_entry::ResourceEntry;

/// Checks for a unique constraint violation (Postgres SQLSTATE 23505 + textual fallback).
pub(super) fn is_unique_violation(err: &sea_orm::DbErr) -> bool {
    use sea_orm::{RuntimeErr, sqlx};
    if let sea_orm::DbErr::Exec(RuntimeErr::SqlxError(arc_err)) = err
        && let sqlx::Error::Database(db_err) = arc_err.as_ref()
        && db_err.code().as_deref() == Some("23505")
    {
        return true;
    }
    let s = err.to_string();
    s.contains("23505")
        || s.contains("duplicate key")
        || s.contains("unique constraint")
        || s.contains("duplicated")
}

/// Returns the Runique built-in `ResourceEntry` (users, rights, groups).
/// Called automatically in the admin registry generated by the daemon.
pub fn builtin_resources() -> Vec<ResourceEntry> {
    vec![
        user::user_entry(),
        droit::droit_entry(),
        groupe::groupe_entry(),
    ]
}
