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 meta DROP COLUMN "enableTwitterIntegration";
            ALTER TABLE meta DROP COLUMN "twitterConsumerKey";
            ALTER TABLE meta DROP COLUMN "twitterConsumerSecret";
            ALTER TABLE meta DROP COLUMN "enableGithubIntegration";
            ALTER TABLE meta DROP COLUMN "githubClientId";
            ALTER TABLE meta DROP COLUMN "githubClientSecret";
            ALTER TABLE meta DROP COLUMN "enableDiscordIntegration";
            ALTER TABLE meta DROP COLUMN "discordClientId";
            ALTER TABLE meta DROP COLUMN "discordClientSecret";

            ALTER TABLE note DROP COLUMN "channelId";
            ALTER TABLE note_unread DROP COLUMN "noteChannelId";

            DROP TABLE channel_note_pining;
            DROP TABLE channel_following;
            DROP TABLE channel;

            DROP TABLE messaging_message;

            ALTER TABLE meta ALTER COLUMN "pinnedPages" SET DEFAULT '{/featured,/explore,/pages,/about-calckey}';            
        "#,
        )
        .await?;

        Ok(())
    }

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

        db.execute_unprepared(r#"
            ALTER TABLE meta ALTER COLUMN "pinnedPages" SET DEFAULT '{/featured,/channels,/explore,/pages,/about-misskey}';   

            create table messaging_message
            (
                id            varchar(32)                                     not null
                    constraint "PK_db398fd79dc95d0eb8c30456eaa"
                        primary key,
                "createdAt"   timestamp with time zone                        not null,
                "userId"      varchar(32)                                     not null
                    constraint "FK_5377c307783fce2b6d352e1203b"
                        references "user"
                        on delete cascade,
                "recipientId" varchar(32)
                    constraint "FK_cac14a4e3944454a5ce7daa5142"
                        references "user"
                        on delete cascade,
                text          varchar(4096),
                "isRead"      boolean       default false                     not null,
                "fileId"      varchar(32)
                    constraint "FK_535def119223ac05ad3fa9ef64b"
                        references drive_file
                        on delete cascade,
                "groupId"     varchar(32)
                    constraint "FK_2c4be03b446884f9e9c502135be"
                        references user_group
                        on delete cascade,
                reads         varchar(32)[] default '{}'::character varying[] not null,
                uri           varchar(512)
            );
            
            comment on column messaging_message."createdAt" is 'The created date of the MessagingMessage.';
            
            comment on column messaging_message."userId" is 'The sender user ID.';
            
            comment on column messaging_message."recipientId" is 'The recipient user ID.';
            
            comment on column messaging_message."groupId" is 'The recipient group ID.';
            
            alter table messaging_message
                owner to "example-calckey-user";
            
            create index "IDX_e21cd3646e52ef9c94aaf17c2e"
                on messaging_message ("createdAt");
            
            create index "IDX_5377c307783fce2b6d352e1203"
                on messaging_message ("userId");
            
            create index "IDX_cac14a4e3944454a5ce7daa514"
                on messaging_message ("recipientId");
            
            create index "IDX_2c4be03b446884f9e9c502135b"
                on messaging_message ("groupId");

            create table channel
            (
                id            varchar(32)              not null
                    constraint "PK_590f33ee6ee7d76437acf362e39"
                        primary key,
                "createdAt"   timestamp with time zone not null,
                "lastNotedAt" timestamp with time zone,
                "userId"      varchar(32)
                    constraint "FK_823bae55bd81b3be6e05cff4383"
                        references "user"
                        on delete set null,
                name          varchar(128)             not null,
                description   varchar(2048),
                "bannerId"    varchar(32)
                    constraint "FK_999da2bcc7efadbfe0e92d3bc19"
                        references drive_file
                        on delete set null,
                "notesCount"  integer default 0        not null,
                "usersCount"  integer default 0        not null
            );
            
            comment on column channel."createdAt" is 'The created date of the Channel.';
            
            comment on column channel."userId" is 'The owner ID.';
            
            comment on column channel.name is 'The name of the Channel.';
            
            comment on column channel.description is 'The description of the Channel.';
            
            comment on column channel."bannerId" is 'The ID of banner Channel.';
            
            comment on column channel."notesCount" is 'The count of notes.';
            
            comment on column channel."usersCount" is 'The count of users.';
            
            alter table channel
                owner to "example-calckey-user";
            
            create index "IDX_71cb7b435b7c0d4843317e7e16"
                on channel ("createdAt");
            
            create index "IDX_29ef80c6f13bcea998447fce43"
                on channel ("lastNotedAt");
            
            create index "IDX_823bae55bd81b3be6e05cff438"
                on channel ("userId");
            
            create index "IDX_0f58c11241e649d2a638a8de94"
                on channel ("notesCount");
            
            create index "IDX_094b86cd36bb805d1aa1e8cc9a"
                on channel ("usersCount");
    
            create table channel_note_pining
            (
                id          varchar(32)              not null
                    constraint "PK_44f7474496bcf2e4b741681146d"
                        primary key,
                "createdAt" timestamp with time zone not null,
                "channelId" varchar(32)              not null
                    constraint "FK_8125f950afd3093acb10d2db8a8"
                        references channel
                        on delete cascade,
                "noteId"    varchar(32)              not null
                    constraint "FK_10b19ef67d297ea9de325cd4502"
                        references note
                        on delete cascade
            );
            
            comment on column channel_note_pining."createdAt" is 'The created date of the ChannelNotePining.';
            
            alter table channel_note_pining
                owner to "example-calckey-user";
            
            create index "IDX_8125f950afd3093acb10d2db8a"
                on channel_note_pining ("channelId");
            
            create unique index "IDX_f36fed37d6d4cdcc68c803cd9c"
                on channel_note_pining ("channelId", "noteId");
    
            create table channel_following
            (
                id           varchar(32)              not null
                    constraint "PK_8b104be7f7415113f2a02cd5bdd"
                        primary key,
                "createdAt"  timestamp with time zone not null,
                "followeeId" varchar(32)              not null
                    constraint "FK_0e43068c3f92cab197c3d3cd86e"
                        references channel
                        on delete cascade,
                "followerId" varchar(32)              not null
                    constraint "FK_6d8084ec9496e7334a4602707e1"
                        references "user"
                        on delete cascade
            );
            
            comment on column channel_following."createdAt" is 'The created date of the ChannelFollowing.';
            
            comment on column channel_following."followeeId" is 'The followee channel ID.';
            
            comment on column channel_following."followerId" is 'The follower user ID.';
            
            alter table channel_following
                owner to "example-calckey-user";
            
            create index "IDX_11e71f2511589dcc8a4d3214f9"
                on channel_following ("createdAt");
            
            create index "IDX_0e43068c3f92cab197c3d3cd86"
                on channel_following ("followeeId");
            
            create index "IDX_6d8084ec9496e7334a4602707e"
                on channel_following ("followerId");
            
            create unique index "IDX_2e230dd45a10e671d781d99f3e"
                on channel_following ("followerId", "followeeId");

            ALTER TABLE "note" ADD COLUMN "channelId" varchar(32)
                CONSTRAINT "FK_f22169eb10657bded6d875ac8f9"
                REFERENCES channel
                ON DELETE CASCADE;

            create index "IDX_f22169eb10657bded6d875ac8f"
                on note ("channelId");
            
            ALTER TABLE "note_unread" ADD COLUMN "noteChannelId" varchar(32);

            create index "IDX_6a57f051d82c6d4036c141e107"
                on note_unread ("noteChannelId");
    
            ALTER TABLE "meta" ADD COLUMN "enableTwitterIntegration" boolean default false not null;
            ALTER TABLE "meta" ADD COLUMN "twitterConsumerKey" varchar(128);
            ALTER TABLE "meta" ADD COLUMN "twitterConsumerSecret" varchar(128);
            ALTER TABLE "meta" ADD COLUMN "enableGithubIntegration" boolean default false not null;
            ALTER TABLE "meta" ADD COLUMN "githubClientId" varchar(128);
            ALTER TABLE "meta" ADD COLUMN "githubClientSecret" varchar(128);
            ALTER TABLE "meta" ADD COLUMN "enableDiscordIntegration" boolean default false not null;
            ALTER TABLE "meta" ADD COLUMN "discordClientId" varchar(128);
            ALTER TABLE "meta" ADD COLUMN "discordClientSecret" varchar(128);
            "#)
            .await?;

        Ok(())
    }
}
