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 db = manager.get_connection();

        db.execute_unprepared(
            r#"
                ALTER TABLE "note" ADD COLUMN "is_renote" BOOLEAN GENERATED ALWAYS AS (
                    "note"."renoteId" IS NOT NULL
                        AND
                    ( 
                        (note.text IS NULL OR note.text = '')
                            AND
                        CARDINALITY(note."fileIds") = 0
                            AND
                        ("note"."cw" IS NULL OR note."cw" = '')
                            AND
                        note."hasPoll" = FALSE
                    )
                ) STORED
            "#,
        )
        .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let db = manager.get_connection();

        db.execute_unprepared(
            r#"
            ALTER TABLE "note" DROP COLUMN "is_renote";
            "#,
        )
        .await?;

        Ok(())
    }
}
