use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[derive(Iden)]
enum BlindIndex {
    Table,
    Id,
    TenantId,
    EntityType,
    EntityId,
    FieldName,
    BlindHash,
    CreatedAt,
    UpdatedAt,
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(BlindIndex::Table)
                    .col(
                        ColumnDef::new(BlindIndex::Id)
                            .uuid()
                            .not_null()
                            .primary_key()
                            .default(PgFunc::gen_random_uuid()),
                    )
                    .col(ColumnDef::new(BlindIndex::TenantId).uuid().not_null())
                    .col(
                        ColumnDef::new(BlindIndex::EntityType)
                            .string_len(64)
                            .not_null(),
                    )
                    .col(ColumnDef::new(BlindIndex::EntityId).uuid().not_null())
                    .col(
                        ColumnDef::new(BlindIndex::FieldName)
                            .string_len(64)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(BlindIndex::BlindHash)
                            .string_len(64)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(BlindIndex::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(BlindIndex::UpdatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .index(
                        Index::create()
                            .col(BlindIndex::TenantId)
                            .col(BlindIndex::EntityType)
                            .col(BlindIndex::FieldName)
                            .col(BlindIndex::BlindHash)
                            .unique(),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_blind_hashes")
                    .table(BlindIndex::Table)
                    .col(BlindIndex::TenantId)
                    .col(BlindIndex::EntityType)
                    .col(BlindIndex::FieldName)
                    .col(BlindIndex::BlindHash)
                    .to_owned(),
            )
            .await
    }

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