//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.3

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

use lldap_domain::types::{Email, UserId, Uuid};

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;

#[derive(Clone, Debug, PartialEq, DeriveModel, Eq, Serialize, Deserialize, DeriveActiveModel)]
#[sea_orm(table_name = "users")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub user_id: UserId,
    pub email: Email,
    pub lowercase_email: String,
    pub display_name: Option<String>,
    pub creation_date: chrono::NaiveDateTime,
    pub password_hash: Option<Vec<u8>>,
    pub totp_secret: Option<String>,
    pub mfa_type: Option<String>,
    pub uuid: Uuid,
    pub modified_date: chrono::NaiveDateTime,
    pub password_modified_date: chrono::NaiveDateTime,
}

impl EntityName for Entity {
    fn table_name(&self) -> &str {
        "users"
    }
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn, PartialEq, Eq, Serialize, Deserialize)]
pub enum Column {
    UserId,
    Email,
    LowercaseEmail,
    DisplayName,
    CreationDate,
    PasswordHash,
    TotpSecret,
    MfaType,
    Uuid,
    ModifiedDate,
    PasswordModifiedDate,
}

impl ColumnTrait for Column {
    type EntityName = Entity;

    fn def(&self) -> ColumnDef {
        match self {
            Column::UserId => ColumnType::String(StringLen::N(255)),
            Column::Email => ColumnType::String(StringLen::N(255)),
            Column::LowercaseEmail => ColumnType::String(StringLen::N(255)),
            Column::DisplayName => ColumnType::String(StringLen::N(255)),
            Column::CreationDate => ColumnType::DateTime,
            Column::PasswordHash => ColumnType::Blob,
            Column::TotpSecret => ColumnType::String(StringLen::N(64)),
            Column::MfaType => ColumnType::String(StringLen::N(64)),
            Column::Uuid => ColumnType::String(StringLen::N(36)),
            Column::ModifiedDate => ColumnType::DateTime,
            Column::PasswordModifiedDate => ColumnType::DateTime,
        }
        .def()
    }
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_many = "super::memberships::Entity")]
    Memberships,
    #[sea_orm(has_many = "super::jwt_refresh_storage::Entity")]
    JwtRefreshStorage,
    #[sea_orm(has_many = "super::jwt_storage::Entity")]
    JwtStorage,
    #[sea_orm(has_many = "super::password_reset_tokens::Entity")]
    PasswordResetTokens,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
    UserId,
}

impl PrimaryKeyTrait for PrimaryKey {
    type ValueType = UserId;

    fn auto_increment() -> bool {
        false
    }
}

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

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

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

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

impl ActiveModelBehavior for ActiveModel {}

impl From<Model> for lldap_domain::types::User {
    fn from(user: Model) -> Self {
        Self {
            user_id: user.user_id,
            email: user.email,
            display_name: user.display_name,
            creation_date: user.creation_date,
            uuid: user.uuid,
            attributes: Vec::new(),
            modified_date: user.modified_date,
            password_modified_date: user.password_modified_date,
        }
    }
}
