import { MigrationInterface, QueryRunner } from 'typeorm';

export class deleteTablesOfSpace1715269347588 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
        DROP TABLE public.post_likes
    `);
    await queryRunner.query(`
        DROP TYPE post_like_type
    `);
    await queryRunner.query(`
        DROP TABLE public.posts
    `);
    await queryRunner.query(`
        DROP INDEX IF EXISTS project_subscribers_users_idx
    `);
    await queryRunner.query(`
        DROP TABLE public.project_subscribers
    `);
    await queryRunner.query(`
        DROP INDEX IF EXISTS post_likes_uniq
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
        create table if not exists public.posts(
            id UUID not null
                constraint post_pk
                    primary key,
            project_id BIGINT not null
                    references public.projects
                    on update cascade on delete restrict,
            created_at timestamptz default now() not null,
            updated_at timestamptz default now() not null,
            deleted_at timestamptz,
            content JSONB,
            parent_id UUID constraint posts_parent_fkey
                references public.posts
                on update cascade on delete CASCADE,
            user_id BIGINT NOT NULL
        );
    `);
    await queryRunner.query(`
        CREATE TYPE post_like_type AS ENUM ('good', 'wow', 'fire', 'smile', 'sweet', 'love', 'heart', 'laugh', 'congratulations', 'balloon', 'eyes', 'cool', 'ok');
    `);
    await queryRunner.query(`
        create table if not exists public.post_likes(
            id UUID not null
                constraint post_likes_pk
                    primary key,
            post_id UUID
                    references public.posts
                    on update cascade on delete restrict,
            created_at timestamptz default now() not null,
            user_id BIGINT NOT NULL,
            like_type post_like_type
        );
    `);

    await queryRunner.query(`
        CREATE UNIQUE INDEX post_likes_uniq ON post_likes(post_id, like_type, user_id);
    `);

    await queryRunner.query(`
        create table project_subscribers
        (
            id uuid PRIMARY KEY,
            project_id bigint  not null
                    references projects
                    on update cascade on delete cascade,
            user_id BIGINT not null,
            created_at timestamptz NOT NULL DEFAULT(NOW()),
            deleted_At timestamptz
        );
    `);

    await queryRunner.query(`
        CREATE INDEX project_subscribers_users_idx ON project_subscribers(project_id, user_id);
    `);
  }
}
