//! `SeaORM` Entity, @generated by sea-orm-codegen

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

/// 租户数据源隔离级别（1: 共享行, 2: 独立表, 3: 独立 Schema, 4: 独立库）
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    EnumIter,
    DeriveActiveEnum,
    serde_repr::Serialize_repr,
    serde_repr::Deserialize_repr,
    JsonSchema,
)]
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
#[repr(i16)]
pub enum TenantIsolationLevel {
    /// 共享行
    #[sea_orm(num_value = 1)]
    SharedRow = 1,
    /// 独立表
    #[sea_orm(num_value = 2)]
    SeparateTable = 2,
    /// 独立 Schema
    #[sea_orm(num_value = 3)]
    SeparateSchema = 3,
    /// 独立库
    #[sea_orm(num_value = 4)]
    SeparateDatabase = 4,
}

/// 租户数据源运行状态
#[derive(
    Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "snake_case")]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(32))")]
pub enum TenantDatasourceStatus {
    /// 启用
    #[sea_orm(string_value = "active")]
    Active,
    /// 停用
    #[sea_orm(string_value = "inactive")]
    Inactive,
    /// 开通中
    #[sea_orm(string_value = "provisioning")]
    Provisioning,
    /// 异常
    #[sea_orm(string_value = "error")]
    Error,
}

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(schema_name = "tenant", table_name = "tenant_datasource")]
pub struct Model {
    /// 主键 ID
    #[sea_orm(primary_key)]
    pub id: i64,
    /// 租户业务唯一标识
    #[sea_orm(unique)]
    pub tenant_id: String,
    /// 隔离级别
    pub isolation_level: TenantIsolationLevel,
    /// 运行状态
    pub status: TenantDatasourceStatus,
    /// 独立 Schema 名称
    #[sea_orm(unique)]
    pub schema_name: Option<String>,
    /// 动态数据源名称
    #[sea_orm(unique)]
    pub datasource_name: Option<String>,
    /// 独立数据库连接串
    pub db_uri: Option<String>,
    /// SQL 日志开关
    pub db_enable_logging: Option<bool>,
    /// 最小连接数
    pub db_min_conns: Option<i32>,
    /// 最大连接数
    pub db_max_conns: Option<i32>,
    /// 连接超时（毫秒）
    pub db_connect_timeout_ms: Option<i64>,
    /// 空闲超时（毫秒）
    pub db_idle_timeout_ms: Option<i64>,
    /// 获取连接超时（毫秒）
    pub db_acquire_timeout_ms: Option<i64>,
    /// 借出前连通性检测
    pub db_test_before_acquire: Option<bool>,
    /// 只读配置
    #[sea_orm(column_type = "JsonBinary")]
    pub readonly_config: Json,
    /// 扩展配置
    #[sea_orm(column_type = "JsonBinary")]
    pub extra_config: Json,
    /// 最近同步时间
    pub last_sync_time: Option<DateTime>,
    /// 备注
    pub remark: String,
    /// 创建人
    pub create_by: String,
    /// 创建时间
    pub create_time: DateTime,
    /// 更新人
    pub update_by: String,
    /// 更新时间
    pub update_time: DateTime,
}

#[sea_orm::entity::prelude::async_trait::async_trait]
impl sea_orm::ActiveModelBehavior for self::ActiveModel {
    async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, sea_orm::DbErr>
    where
        C: sea_orm::ConnectionTrait,
    {
        let now = chrono::Local::now().naive_local();
        self.update_time = sea_orm::Set(now);
        if insert {
            self.create_time = sea_orm::Set(now);
        }
        Ok(self)
    }
}
