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

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

use crate::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,
}

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,
}

impl ColumnTrait for Column {
    type EntityName = Entity;

    fn def(&self) -> ColumnDef {
        match self {
            Column::UserId => ColumnType::String(Some(255)),
            Column::Email => ColumnType::String(Some(255)),
            Column::LowercaseEmail => ColumnType::String(Some(255)),
            Column::DisplayName => ColumnType::String(Some(255)),
            Column::CreationDate => ColumnType::DateTime,
            Column::PasswordHash => ColumnType::Binary(BlobSize::Medium),
            Column::TotpSecret => ColumnType::String(Some(64)),
            Column::MfaType => ColumnType::String(Some(64)),
            Column::Uuid => ColumnType::String(Some(36)),
        }
        .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 crate::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(),
        }
    }
}
