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", "music_enabled").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(
                            ColumnDef::new(Alias::new("music_enabled"))
                                .boolean()
                                .null()
                                .default(true),
                        )
                        .to_owned(),
                )
                .await?;
        }

        Ok(())
    }

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

        if manager.has_column("site_settings", "music_enabled").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(table)
                        .drop_column(Alias::new("music_enabled"))
                        .to_owned(),
                )
                .await?;
        }

        Ok(())
    }
}
