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

use super::RoomMemberRole;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "room_member")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub room: RoomId,
    #[sea_orm(primary_key)]
    pub user: UserId,
    pub role: RoomMemberRole,
    pub first_msg_in: Option<DateTimeUtc>,
    pub joined_at: Option<DateTimeUtc>,
    pub last_read_seq: Option<Seq>,
    /// Do Not Disturb: if true, suppress notifications for this room
    pub do_not_disturb: bool,
    /// DND start hour (0-23, local time). None means no scheduled DND.
    pub dnd_start_hour: Option<i16>,
    /// DND end hour (0-23, local time). None means no scheduled DND.
    pub dnd_end_hour: Option<i16>,
}

impl Model {
    #[deprecated(since = "0.1.0", note = "role is now an enum")]
    pub fn role_enum(&self) -> Result<RoomMemberRole, &'static str> {
        Ok(self.role.clone())
    }
}

#[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,
}

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

impl ActiveModelBehavior for ActiveModel {}
