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(TenantCryptoKey::Table)
                    .col(
                        ColumnDef::new(TenantCryptoKey::Id)
                            .uuid()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(TenantCryptoKey::TenantId).uuid().not_null())
                    .col(
                        ColumnDef::new(TenantCryptoKey::EncryptedDek)
                            .string_len(128)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(TenantCryptoKey::KeyVersion)
                            .integer()
                            .not_null()
                            .default(1),
                    )
                    .col(
                        ColumnDef::new(TenantCryptoKey::IsActive)
                            .boolean()
                            .not_null()
                            .default(true),
                    )
                    .col(
                        ColumnDef::new(TenantCryptoKey::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(TenantCryptoKey::UpdatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(ColumnDef::new(TenantCryptoKey::CreatedBy).uuid())
                    .col(ColumnDef::new(TenantCryptoKey::UpdatedBy).uuid())
                    .col(ColumnDef::new(TenantCryptoKey::DeletedAt).timestamp_with_time_zone())
                    .col(
                        ColumnDef::new(TenantCryptoKey::Version)
                            .integer()
                            .not_null()
                            .default(1),
                    )
                    .index(
                        Index::create()
                            .col(TenantCryptoKey::TenantId)
                            .col(TenantCryptoKey::KeyVersion)
                            .unique(),
                    )
                    .to_owned(),
            )
            .await?;

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

        Ok(())
    }

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

#[derive(DeriveIden)]
enum TenantCryptoKey {
    Table,
    Id,
    TenantId,
    EncryptedDek,
    KeyVersion,
    IsActive,
    CreatedAt,
    UpdatedAt,
    CreatedBy,
    UpdatedBy,
    DeletedAt,
    Version,
}
