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
            .alter_table(
                Table::alter()
                    .table(Alias::new("subscriptions"))
                    .add_column_if_not_exists(
                        ColumnDef::new(Alias::new("confirm_token"))
                            .string()
                            .null(),
                    )
                    .add_column_if_not_exists(
                        ColumnDef::new(Alias::new("manage_token"))
                            .string()
                            .null(),
                    )
                    .add_column_if_not_exists(
                        ColumnDef::new(Alias::new("metadata"))
                            .json_binary()
                            .null(),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_subscriptions_confirm_token_unique")
                    .table(Alias::new("subscriptions"))
                    .col(Alias::new("confirm_token"))
                    .unique()
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_subscriptions_manage_token_unique")
                    .table(Alias::new("subscriptions"))
                    .col(Alias::new("manage_token"))
                    .unique()
                    .to_owned(),
            )
            .await?;

        manager
            .alter_table(
                Table::alter()
                    .table(Alias::new("notification_deliveries"))
                    .add_column_if_not_exists(
                        ColumnDef::new(Alias::new("attempts_count"))
                            .integer()
                            .not_null()
                            .default(0),
                    )
                    .add_column_if_not_exists(
                        ColumnDef::new(Alias::new("next_retry_at"))
                            .string()
                            .null(),
                    )
                    .add_column_if_not_exists(
                        ColumnDef::new(Alias::new("last_attempt_at"))
                            .string()
                            .null(),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_notification_deliveries_status_next_retry")
                    .table(Alias::new("notification_deliveries"))
                    .col(Alias::new("status"))
                    .col(Alias::new("next_retry_at"))
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_index(
                Index::drop()
                    .name("idx_notification_deliveries_status_next_retry")
                    .table(Alias::new("notification_deliveries"))
                    .to_owned(),
            )
            .await?;

        manager
            .alter_table(
                Table::alter()
                    .table(Alias::new("notification_deliveries"))
                    .drop_column(Alias::new("last_attempt_at"))
                    .drop_column(Alias::new("next_retry_at"))
                    .drop_column(Alias::new("attempts_count"))
                    .to_owned(),
            )
            .await?;

        manager
            .drop_index(
                Index::drop()
                    .name("idx_subscriptions_manage_token_unique")
                    .table(Alias::new("subscriptions"))
                    .to_owned(),
            )
            .await?;

        manager
            .drop_index(
                Index::drop()
                    .name("idx_subscriptions_confirm_token_unique")
                    .table(Alias::new("subscriptions"))
                    .to_owned(),
            )
            .await?;

        manager
            .alter_table(
                Table::alter()
                    .table(Alias::new("subscriptions"))
                    .drop_column(Alias::new("metadata"))
                    .drop_column(Alias::new("manage_token"))
                    .drop_column(Alias::new("confirm_token"))
                    .to_owned(),
            )
            .await?;

        Ok(())
    }
}
