use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(UserCredentials::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(UserCredentials::Id)
                            .uuid()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(UserCredentials::TenantId).uuid().not_null())
                    .col(ColumnDef::new(UserCredentials::UserId).uuid().not_null())
                    .col(
                        ColumnDef::new(UserCredentials::CredentialType)
                            .string()
                            .not_null()
                            .default("password"),
                    )
                    .col(
                        ColumnDef::new(UserCredentials::CredentialData)
                            .json()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(UserCredentials::Verified)
                            .boolean()
                            .not_null()
                            .default(false),
                    )
                    .col(
                        ColumnDef::new(UserCredentials::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(UserCredentials::UpdatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(ColumnDef::new(UserCredentials::CreatedBy).uuid().not_null())
                    .col(ColumnDef::new(UserCredentials::UpdatedBy).uuid().not_null())
                    .col(
                        ColumnDef::new(UserCredentials::DeletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(UserCredentials::Version)
                            .integer()
                            .not_null()
                            .default(1),
                    )
                    .foreign_key(
                        &mut ForeignKey::create()
                            .name("fk_user_credentials_user_id")
                            .from(UserCredentials::Table, UserCredentials::UserId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .to_owned(),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_user_credentials_tenant_id")
                    .table(UserCredentials::Table)
                    .col(UserCredentials::TenantId)
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_user_credentials_user_id")
                    .table(UserCredentials::Table)
                    .col(UserCredentials::UserId)
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(UserCredentials::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum UserCredentials {
    Table,
    Id,
    TenantId,
    UserId,
    CredentialType,
    CredentialData,
    Verified,
    CreatedAt,
    UpdatedAt,
    CreatedBy,
    UpdatedBy,
    DeletedAt,
    Version,
}

#[derive(DeriveIden)]
enum Users {
    Table,
    Id,
}
