use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "user")]
pub struct Model {
  #[sea_orm(primary_key, auto_increment = false)]
  pub id: Uuid,
  pub name: String,
  #[sea_orm(unique)]
  pub email: String,
  pub password: String,
  pub salt: String,
  pub oidc_user: bool,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
  #[sea_orm(has_many = "super::group_user::Entity")]
  GroupUser,
  #[cfg(feature = "avatar")]
  #[sea_orm(has_one = "super::user_avatar::Entity")]
  UserAvatar,
}

impl Related<super::group_user::Entity> for Entity {
  fn to() -> RelationDef {
    Relation::GroupUser.def()
  }
}

#[cfg(feature = "avatar")]
impl Related<super::user_avatar::Entity> for Entity {
  fn to() -> RelationDef {
    Relation::UserAvatar.def()
  }
}

impl Related<super::group::Entity> for Entity {
  fn to() -> RelationDef {
    super::group_user::Relation::Group.def()
  }
  fn via() -> Option<RelationDef> {
    Some(super::group_user::Relation::User.def().rev())
  }
}

impl ActiveModelBehavior for ActiveModel {}
