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_quote" BOOLEAN GENERATED ALWAYS AS (
                    "note"."renoteId" IS NOT NULL
                        AND
                    ( 
                        note.text IS NOT NULL AND note.text != ''
                            OR
                        CARDINALITY(note."fileIds") != 0
                            OR
                        "note"."cw" IS NOT NULL AND note."cw" != ''
                            OR
                        note."hasPoll" = TRUE
                    )
                ) 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_quote";
            "#,
        )
        .await?;

        Ok(())
    }
}
