use crate::{DateTimeUtc, RoomId, RoomThreadId, Seq, UserId};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "room_thread")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: RoomThreadId,
    pub room: RoomId,
    pub parent: Seq,
    pub created_by: UserId,
    pub participants: sea_orm::JsonValue,
    pub last_message_at: DateTimeUtc,
    pub last_message_preview: Option<String>,
    pub created_at: DateTimeUtc,
    pub updated_at: DateTimeUtc,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::room::Entity",
        from = "Column::Room",
        to = "super::room::Column::Id"
    )]
    Room,
    #[sea_orm(has_many = "super::room_message::Entity")]
    Messages,
}

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

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

impl ActiveModelBehavior for ActiveModel {}
