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#"
            DROP TABLE "gallery_like";
            DROP TABLE "gallery_post";
            "#,
        )
        .await?;

        Ok(())
    }

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

        db.execute_unprepared(
            r#"
            create table gallery_post
            (
                id            varchar(32)                                      not null
                    constraint "PK_8e90d7b6015f2c4518881b14753"
                        primary key,
                "createdAt"   timestamp with time zone                         not null,
                "updatedAt"   timestamp with time zone                         not null,
                title         varchar(256)                                     not null,
                description   varchar(2048),
                "userId"      varchar(32)                                      not null
                    constraint "FK_985b836dddd8615e432d7043ddb"
                        references "user"
                        on delete cascade,
                "fileIds"     varchar(32)[]  default '{}'::character varying[] not null,
                "isSensitive" boolean        default false                     not null,
                "likedCount"  integer        default 0                         not null,
                tags          varchar(128)[] default '{}'::character varying[] not null
            );
            
            comment on column gallery_post."createdAt" is 'The created date of the GalleryPost.';
            
            comment on column gallery_post."updatedAt" is 'The updated date of the GalleryPost.';
            
            comment on column gallery_post."userId" is 'The ID of author.';
            
            comment on column gallery_post."isSensitive" is 'Whether the post is sensitive.';
                        
            create index "IDX_8f1a239bd077c8864a20c62c2c"
                on gallery_post ("createdAt");
            
            create index "IDX_f631d37835adb04792e361807c"
                on gallery_post ("updatedAt");
            
            create index "IDX_985b836dddd8615e432d7043dd"
                on gallery_post ("userId");
            
            create index "IDX_3ca50563facd913c425e7a89ee"
                on gallery_post ("fileIds");
            
            create index "IDX_f2d744d9a14d0dfb8b96cb7fc5"
                on gallery_post ("isSensitive");
            
            create index "IDX_1a165c68a49d08f11caffbd206"
                on gallery_post ("likedCount");
            
            create index "IDX_05cca34b985d1b8edc1d1e28df"
                on gallery_post (tags);

            create table gallery_like
            (
                id          varchar(32)              not null
                    constraint "PK_853ab02be39b8de45cd720cc15f"
                        primary key,
                "createdAt" timestamp with time zone not null,
                "userId"    varchar(32)              not null
                    constraint "FK_8fd5215095473061855ceb948cf"
                        references "user"
                        on delete cascade,
                "postId"    varchar(32)              not null
                    constraint "FK_b1cb568bfe569e47b7051699fc8"
                        references gallery_post
                        on delete cascade
            );
            
            create index "IDX_8fd5215095473061855ceb948c"
                on gallery_like ("userId");
            
            create unique index "IDX_df1b5f4099e99fb0bc5eae53b6"
                on gallery_like ("userId", "postId");
            "#,
        )
        .await?;

        Ok(())
    }
}
