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

/// 邮件附件（小附件 inline 存 content；大附件后续可落盘存 storage_path）
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "email_attachments")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    /// 所属邮件 ID（FK emails.id）
    pub email_id: i64,
    pub filename: Option<String>,
    pub content_type: String,
    pub size_bytes: i64,
    /// 二进制内容（小附件）
    pub content: Option<Vec<u8>>,
    pub created_at: DateTime,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

#[async_trait::async_trait]
impl ActiveModelBehavior for ActiveModel {
    async fn before_save<C: ConnectionTrait>(self, _: &C, insert: bool) -> Result<Self, DbErr> {
        let mut value = self;
        if insert {
            value.created_at = Set(chrono::Utc::now().naive_utc());
        }
        Ok(value)
    }
}
