import { MigrationInterface, QueryRunner } from 'typeorm';

export class triggerWebhooks1707893218989 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE projects ADD COLUMN webhook_url varchar`,
    );
    await queryRunner.query(
      `CREATE TABLE event_triggers(
        project_id BIGINT,
        id UUID,
        filter_type_ids UUID[],
        filter_name varchar,
        target_block_key BIGINT,
        target_prop VARCHAR,
        event_name VARCHAR,

        PRIMARY KEY(project_id, id),
        CONSTRAINT event_triggers_project_id_target_block_key_fkey FOREIGN KEY (project_id, target_block_key)
            references asset_block_keys(project_id, block_key)  ON UPDATE CASCADE ON DELETE CASCADE
      )`,
    );
    await queryRunner.query(`
        create table event_actions
        (
            id uuid,
            project_id bigint not null,
            asset_id uuid not null,
            target_block_key BIGINT,
            target_prop VARCHAR,
            old_target_content JSONB, 
            new_target_content JSONB,
            event_name varchar,
            created_at timestamptz NOT NULL DEFAULT(NOW()),
            processed_at timestamptz,
            PRIMARY KEY(project_id, id),
            CONSTRAINT event_actions_project_id_target_block_key_fkey FOREIGN KEY (project_id, target_block_key)
              references asset_block_keys(project_id, block_key)  ON UPDATE CASCADE ON DELETE CASCADE,
            CONSTRAINT event_actions_project_id_asset_id foreign key (project_id, asset_id) 
              references assets(project_id, id) ON UPDATE CASCADE ON DELETE CASCADE
        );
    `);
    await queryRunner.query(`
        CREATE INDEX event_actions_created_idx ON event_actions(project_id, created_at)
        WHERE processed_at IS NULL
    `);
    await queryRunner.query(`
        create table event_webhook_logs
        (
            id uuid,
            project_id   bigint not null,
            actions_from timestamptz,
            actions_to   timestamptz,
            request_at   timestamptz NOT NULL DEFAULT(NOW()),
            response_at  timestamptz,
            response_status_code smallint,
            attempt smallInt default 0,
            response_error text,
            PRIMARY KEY(project_id, id)
        );
    `);
    await queryRunner.query(`
        CREATE INDEX event_webhook_logs_request_at_idx ON event_webhook_logs(project_id, request_at)
    `);
    await queryRunner.query(`
        CREATE INDEX event_webhook_logs_actions_to_idx ON event_webhook_logs(project_id, actions_to)
        WHERE (response_status_code IS NULL OR (response_status_code >= 200 AND response_status_code < 300))
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TABLE projects DROP COLUMN webhook_url`);
    await queryRunner.query(`DROP table event_actions`);
    await queryRunner.query(`DROP table event_triggers`);
    await queryRunner.query(`DROP table event_webhook_logs`);
  }
}
