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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "room")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: RoomId,
    pub project: ProjectId,
    pub room_name: String,
    #[sea_orm(column_name = "public")]
    pub public: bool,
    pub category: Option<RoomCategoryId>,
    pub created_by: UserId,
    pub created_at: DateTimeUtc,
    pub last_msg_at: DateTimeUtc,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::room_category::Entity",
        from = "Column::Category",
        to = "super::room_category::Column::Id"
    )]
    Category,
    #[sea_orm(has_many = "super::room_message::Entity")]
    Messages,
    #[sea_orm(has_many = "super::room_member::Entity")]
    Members,
    #[sea_orm(has_many = "super::room_thread::Entity")]
    Threads,
    #[sea_orm(has_many = "super::room_ai::Entity")]
    Ais,
    #[sea_orm(has_many = "super::room_pin::Entity")]
    Pins,
}

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

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

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

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

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

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

impl ActiveModelBehavior for ActiveModel {}
