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("posts");

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

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

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

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

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

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

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

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

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

        Ok(())
    }

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

        for column in [
            "redirect_to",
            "redirect_from",
            "og_image",
            "noindex",
            "canonical_url",
            "unpublish_at",
            "publish_at",
            "visibility",
            "status",
        ] {
            if manager.has_column("posts", column).await? {
                manager
                    .alter_table(
                        Table::alter()
                            .table(table.clone())
                            .drop_column(Alias::new(column))
                            .to_owned(),
                    )
                    .await?;
            }
        }

        Ok(())
    }
}
