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

/// 来源 IP 黑名单（单 IP 或 CIDR，应用层匹配）
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "blocked_ips")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub ip: String,
    pub reason: String,
    /// manual | auto
    pub source: String,
    pub expires_at: Option<DateTime>,
    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)
    }
}
