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(MessageSubscriptions::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(MessageSubscriptions::Id)
                            .uuid()
                            .not_null()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::TenantId)
                            .uuid()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::UserId)
                            .uuid()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::NotificationTypes)
                            .json()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::ChannelPreferences)
                            .json()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::DndEnabled)
                            .boolean()
                            .not_null()
                            .default(false),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::DndStart)
                            .string()
                            .null(),
                    )
                    .col(ColumnDef::new(MessageSubscriptions::DndEnd).string().null())
                    .col(
                        ColumnDef::new(MessageSubscriptions::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::UpdatedAt)
                            .timestamp_with_time_zone()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::CreatedBy)
                            .uuid()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::UpdatedBy)
                            .uuid()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(MessageSubscriptions::DeletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .to_owned(),
            )
            .await?;

        manager.get_connection().execute(sea_orm::Statement::from_string(
            sea_orm::DatabaseBackend::Postgres,
            "CREATE UNIQUE INDEX idx_message_subscriptions_tenant_user ON message_subscriptions (tenant_id, user_id) WHERE deleted_at IS NULL".to_string(),
        )).await.map_err(|e| DbErr::Custom(e.to_string()))?;

        Ok(())
    }

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

#[derive(DeriveIden)]
enum MessageSubscriptions {
    Table,
    Id,
    TenantId,
    UserId,
    NotificationTypes,
    ChannelPreferences,
    DndEnabled,
    DndStart,
    DndEnd,
    CreatedAt,
    UpdatedAt,
    CreatedBy,
    UpdatedBy,
    DeletedAt,
}
