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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "credit_rules")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: i32,
    pub register_bonus: i64,
    pub daily_check_in_reward: i64,
    pub reward_balance_cap: i64,
    pub create_mailbox_cost: i64,
    pub receive_email_cost: i64,
    pub daily_mailboxes_limit: i32,
    pub daily_emails_limit: i32,
    pub version: i64,
    pub updated_by: Option<String>,
    pub created_at: DateTime,
    pub updated_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;
        let now = chrono::Utc::now().naive_utc();
        if insert {
            value.created_at = Set(now);
        }
        value.updated_at = Set(now);
        Ok(value)
    }
}
