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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "messages")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: Uuid,
    pub tenant_id: Uuid,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub template_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sender_id: Option<Uuid>,
    pub sender_type: String,
    pub recipient_id: Uuid,
    pub recipient_type: String,
    pub title: String,
    pub body: String,
    pub priority: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub business_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub business_id: Option<Uuid>,
    pub is_read: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub read_at: Option<DateTimeUtc>,
    pub is_archived: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub archived_at: Option<DateTimeUtc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sent_at: Option<DateTimeUtc>,
    pub status: String,
    pub created_at: DateTimeUtc,
    pub updated_at: DateTimeUtc,
    pub created_by: Uuid,
    pub updated_by: Uuid,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deleted_at: Option<DateTimeUtc>,
    pub version: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::message_template::Entity",
        from = "Column::TemplateId",
        to = "super::message_template::Column::Id"
    )]
    MessageTemplate,
}

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

impl ActiveModelBehavior for ActiveModel {}
