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#"
            DELETE FROM "antenna" WHERE "src" = 'group';

            ALTER TYPE "antenna_src_enum" RENAME TO "antenna_src_enum_old";
            CREATE TYPE "antenna_src_enum" AS ENUM('home', 'all', 'users', 'list', 'instances');
            ALTER TABLE "antenna" ALTER COLUMN "src" TYPE "antenna_src_enum" USING "src"::text::antenna_src_enum;
            DROP TYPE "antenna_src_enum_old";

            ALTER TABLE "antenna" DROP COLUMN "userGroupJoiningId";

            ALTER TABLE "notification" DROP COLUMN "userGroupInvitationId";

            ALTER TABLE "user_profile" ALTER COLUMN "emailNotificationTypes"
                SET DEFAULT '["follow", "receiveFollowRequest"]'::jsonb;

            DROP TABLE "user_group_invitation";
            DROP TABLE "user_group_joining";
            DROP TABLE "user_group_invite";
            DROP TABLE "user_group";
            "#,
        )
        .await?;

        Ok(())
    }

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

        db.execute_unprepared(
            r#"
            create table user_group
            (
                id          varchar(32)              not null
                    constraint "PK_3c29fba6fe013ec8724378ce7c9"
                        primary key,
                "createdAt" timestamp with time zone not null,
                name        varchar(256)             not null,
                "userId"    varchar(32)              not null
                    constraint "FK_3d6b372788ab01be58853003c93"
                        references "user"
                        on delete cascade,
                "isPrivate" boolean default false    not null
            );
            
            comment on column user_group."createdAt" is 'The created date of the UserGroup.';
            
            comment on column user_group."userId" is 'The ID of owner.';
                        
            create index "IDX_20e30aa35180e317e133d75316"
                on user_group ("createdAt");
            
            create index "IDX_3d6b372788ab01be58853003c9"
                on user_group ("userId");
            
            create table user_group_invitation
            (
                id            varchar(32)              not null
                    constraint "PK_160c63ec02bf23f6a5c5e8140d6"
                        primary key,
                "createdAt"   timestamp with time zone not null,
                "userId"      varchar(32)              not null
                    constraint "FK_bfbc6305547539369fe73eb144a"
                        references "user"
                        on delete cascade,
                "userGroupId" varchar(32)              not null
                    constraint "FK_5cc8c468090e129857e9fecce5a"
                        references user_group
                        on delete cascade
            );
            
            comment on column user_group_invitation."createdAt" is 'The created date of the UserGroupInvitation.';
            
            comment on column user_group_invitation."userId" is 'The user ID.';
            
            comment on column user_group_invitation."userGroupId" is 'The group ID.';
                        
            create index "IDX_bfbc6305547539369fe73eb144"
                on user_group_invitation ("userId");
            
            create index "IDX_5cc8c468090e129857e9fecce5"
                on user_group_invitation ("userGroupId");
            
            create unique index "IDX_e9793f65f504e5a31fbaedbf2f"
                on user_group_invitation ("userId", "userGroupId");

            create table user_group_joining
            (
                id            varchar(32)              not null
                    constraint "PK_15f2425885253c5507e1599cfe7"
                        primary key,
                "createdAt"   timestamp with time zone not null,
                "userId"      varchar(32)              not null
                    constraint "FK_f3a1b4bd0c7cabba958a0c0b231"
                        references "user"
                        on delete cascade,
                "userGroupId" varchar(32)              not null
                    constraint "FK_67dc758bc0566985d1b3d399865"
                        references user_group
                        on delete cascade
            );
            
            comment on column user_group_joining."createdAt" is 'The created date of the UserGroupJoining.';
            
            comment on column user_group_joining."userId" is 'The user ID.';
            
            comment on column user_group_joining."userGroupId" is 'The group ID.';
                        
            create index "IDX_f3a1b4bd0c7cabba958a0c0b23"
                on user_group_joining ("userId");
            
            create index "IDX_67dc758bc0566985d1b3d39986"
                on user_group_joining ("userGroupId");
            
            create unique index "IDX_d9ecaed8c6dc43f3592c229282"
                on user_group_joining ("userId", "userGroupId");
            

            create table user_group_invite
            (
                id            varchar(32)              not null
                    constraint "PK_3893884af0d3a5f4d01e7921a97"
                        primary key,
                "createdAt"   timestamp with time zone not null,
                "userId"      varchar(32)              not null
                    constraint "FK_1039988afa3bf991185b277fe03"
                        references "user"
                        on delete cascade,
                "userGroupId" varchar(32)              not null
                    constraint "FK_e10924607d058004304611a436a"
                        references user_group
                        on delete cascade
            );
            
            alter table user_group_invite
                owner to "example-calckey-user";
            
            create index "IDX_1039988afa3bf991185b277fe0"
                on user_group_invite ("userId");
            
            create index "IDX_e10924607d058004304611a436"
                on user_group_invite ("userGroupId");
            
            create unique index "IDX_78787741f9010886796f2320a4"
                on user_group_invite ("userId", "userGroupId");

            ALTER TABLE "user_profile" ALTER COLUMN "emailNotificationTypes"
                SET DEFAULT '["follow", "receiveFollowRequest", "groupInvited"]'::jsonb;

            alter table antenna add "userGroupJoiningId" varchar(32)
                constraint "FK_ccbf5a8c0be4511133dcc50ddeb"
                    references user_group_joining
                    on delete cascade;
                
            alter table notification add column "userGroupInvitationId" varchar(32)
                constraint "FK_8fe87814e978053a53b1beb7e98"
                    references user_group_invitation
                    on delete cascade;

            ALTER TYPE "antenna_src_enum" RENAME TO "antenna_src_enum_old";
            CREATE TYPE "antenna_src_enum" AS ENUM('home', 'all', 'users', 'list', 'group', 'instances');
            ALTER TABLE "antenna" ALTER COLUMN "src" TYPE "antenna_src_enum" USING "src"::text::antenna_src_enum;
            DROP TYPE "antenna_src_enum_old";
            "#,
        )
        .await?;

        Ok(())
    }
}
