import { MigrationInterface, QueryRunner } from 'typeorm';

export class notifications1704951919595 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
            CREATE TABLE notifications(
              id uuid PRIMARY KEY,
              project_id BIGINT NOT NULL references projects(id) ON UPDATE CASCADE ON DELETE CASCADE,
              user_id integer NOT NULL,
              content JSONB NOT NULL,
              created_at  timestamp with time zone default now() not null,
              viewed_at  timestamp with time zone,
              deleted_at  timestamp with time zone  
            )          
        `);

    await queryRunner.query(`
        CREATE INDEX notification_user_idx ON notifications(user_id, created_at DESC)
    `);
    await queryRunner.query(`
        CREATE INDEX notification_user_unread_idx ON notifications(user_id, created_at DESC) WHERE viewed_at IS NULL
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
            DROP TABLE notifications
        `);
  }
}
