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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "process_definitions")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: Uuid,
    pub tenant_id: Uuid,
    pub name: String,
    pub key: String,
    /// 业务版本号（如同一 key 可存在多个版本），当前固定为 1，未来支持发布新版本时递增。
    pub version: i32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    pub nodes: serde_json::Value,
    pub edges: serde_json::Value,
    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>,
    /// 乐观锁版本号（ERP 标准审计字段），每次更新递增。
    pub version_field: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_many = "super::process_instance::Entity")]
    ProcessInstance,
}

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

impl ActiveModelBehavior for ActiveModel {}
