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 "user_profile" DROP COLUMN "pinnedPageId";

            DROP TABLE "page_like";
            DROP TABLE "page";
            "#,
        )
        .await?;

        Ok(())
    }

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

        db.execute_unprepared(
            r#"
            create table page
            (
                id                    varchar(32)                                      not null
                    constraint "PK_742f4117e065c5b6ad21b37ba1f"
                        primary key,
                "createdAt"           timestamp with time zone                         not null,
                "updatedAt"           timestamp with time zone                         not null,
                title                 varchar(256)                                     not null,
                name                  varchar(256)                                     not null,
                summary               varchar(256),
                "alignCenter"         boolean                                          not null,
                font                  varchar(32)                                      not null,
                "userId"              varchar(32)                                      not null
                    constraint "FK_ae1d917992dd0c9d9bbdad06c4a"
                        references "user"
                        on delete cascade,
                "eyeCatchingImageId"  varchar(32)
                    constraint "FK_a9ca79ad939bf06066b81c9d3aa"
                        references drive_file
                        on delete cascade,
                content               jsonb          default '[]'::jsonb               not null,
                variables             jsonb          default '[]'::jsonb               not null,
                visibility            page_visibility_enum                             not null,
                "visibleUserIds"      varchar(32)[]  default '{}'::character varying[] not null,
                "likedCount"          integer        default 0                         not null,
                "hideTitleWhenPinned" boolean        default false                     not null,
                script                varchar(16384) default ''::character varying     not null,
                "isPublic"            boolean        default true                      not null
            );
            
            comment on column page."createdAt" is 'The created date of the Page.';
            
            comment on column page."updatedAt" is 'The updated date of the Page.';
            
            comment on column page."userId" is 'The ID of author.';
                        
            create index "IDX_fbb4297c927a9b85e9cefa2eb1"
                on page ("createdAt");
            
            create index "IDX_af639b066dfbca78b01a920f8a"
                on page ("updatedAt");
            
            create index "IDX_b82c19c08afb292de4600d99e4"
                on page (name);
            
            create index "IDX_ae1d917992dd0c9d9bbdad06c4"
                on page ("userId");
            
            create index "IDX_90148bbc2bf0854428786bfc15"
                on page ("visibleUserIds");
            
            create unique index "IDX_2133ef8317e4bdb839c0dcbf13"
                on page ("userId", name);

            create table page_like
            (
                id          varchar(32)              not null
                    constraint "PK_813f034843af992d3ae0f43c64c"
                        primary key,
                "createdAt" timestamp with time zone not null,
                "userId"    varchar(32)              not null
                    constraint "FK_0e61efab7f88dbb79c9166dbb48"
                        references "user"
                        on delete cascade,
                "pageId"    varchar(32)              not null
                    constraint "FK_cf8782626dced3176038176a847"
                        references page
                        on delete cascade
            );
            
            create index "IDX_0e61efab7f88dbb79c9166dbb4"
                on page_like ("userId");
            
            create unique index "IDX_4ce6fb9c70529b4c8ac46c9bfa"
                on page_like ("userId", "pageId");

            alter table user_profile
                add "pinnedPageId" varchar(32)
                    constraint "UQ_6dc44f1ceb65b1e72bacef2ca27"
                        unique
                    constraint "FK_6dc44f1ceb65b1e72bacef2ca27"
                        references page
                        on delete set null;
            "#,
        )
        .await?;

        Ok(())
    }
}
