//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.3

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

use crate::domain::types::{GroupId, UserId};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "memberships")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub user_id: UserId,
    #[sea_orm(primary_key)]
    pub group_id: GroupId,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::groups::Entity",
        from = "Column::GroupId",
        to = "super::groups::Column::GroupId",
        on_update = "Cascade",
        on_delete = "Cascade"
    )]
    Groups,
    #[sea_orm(
        belongs_to = "super::users::Entity",
        from = "Column::UserId",
        to = "super::users::Column::UserId",
        on_update = "Cascade",
        on_delete = "Cascade"
    )]
    Users,
}

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

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

#[derive(Debug)]
pub struct UserToGroup;
impl Linked for UserToGroup {
    type FromEntity = super::User;

    type ToEntity = super::Group;

    fn link(&self) -> Vec<RelationDef> {
        vec![Relation::Users.def().rev(), Relation::Groups.def()]
    }
}

#[derive(Debug)]
pub struct GroupToUser;
impl Linked for GroupToUser {
    type FromEntity = super::Group;

    type ToEntity = super::User;

    fn link(&self) -> Vec<RelationDef> {
        vec![Relation::Groups.def().rev(), Relation::Users.def()]
    }
}

impl ActiveModelBehavior for ActiveModel {}
