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> {
        let table = Alias::new("site_settings");

        if !manager
            .has_column("site_settings", "subscription_popup_enabled")
            .await?
        {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(
                            ColumnDef::new(Alias::new("subscription_popup_enabled"))
                                .boolean()
                                .null()
                                .default(true),
                        )
                        .to_owned(),
                )
                .await?;
        }

        if !manager
            .has_column("site_settings", "subscription_popup_title")
            .await?
        {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(
                            ColumnDef::new(Alias::new("subscription_popup_title"))
                                .string()
                                .null(),
                        )
                        .to_owned(),
                )
                .await?;
        }

        if !manager
            .has_column("site_settings", "subscription_popup_description")
            .await?
        {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(
                            ColumnDef::new(Alias::new("subscription_popup_description"))
                                .text()
                                .null(),
                        )
                        .to_owned(),
                )
                .await?;
        }

        if !manager
            .has_column("site_settings", "subscription_popup_delay_seconds")
            .await?
        {
            manager
                .alter_table(
                    Table::alter()
                        .table(table)
                        .add_column(
                            ColumnDef::new(Alias::new("subscription_popup_delay_seconds"))
                                .integer()
                                .null()
                                .default(18),
                        )
                        .to_owned(),
                )
                .await?;
        }

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let table = Alias::new("site_settings");

        for column in [
            "subscription_popup_delay_seconds",
            "subscription_popup_description",
            "subscription_popup_title",
            "subscription_popup_enabled",
        ] {
            if manager.has_column("site_settings", column).await? {
                manager
                    .alter_table(
                        Table::alter()
                            .table(table.clone())
                            .drop_column(Alias::new(column))
                            .to_owned(),
                    )
                    .await?;
            }
        }

        Ok(())
    }
}
