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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "group")]
pub struct Model {
  #[sea_orm(primary_key, auto_increment = false)]
  pub id: Uuid,
  pub name: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
  #[sea_orm(has_many = "super::group_permission::Entity")]
  GroupPermission,
  #[sea_orm(has_many = "super::group_user::Entity")]
  GroupUser,
}

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

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

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

impl ActiveModelBehavior for ActiveModel {}
