import { MigrationInterface, QueryRunner } from 'typeorm';

export class updateComps1697957432764 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
        alter table asset_blocks
        drop column readiness_scale;
    `);
    await queryRunner.query(`
        alter table asset_blocks
        drop column deleted_at;
    `);
    await queryRunner.query(`
        alter table asset_link_comps
        drop constraint asset_link_comps_project_id_asset_id_fkey;
    `);
    await queryRunner.query(`
        alter table asset_link_comps
        drop constraint asset_link_comps_project_id_block_key_fkey;
    `);
    await queryRunner.query(`
        alter table asset_link_comps
        drop constraint asset_link_comps_project_id_target_asset_id_fkey;
    `);
    await queryRunner.query(`
        alter table asset_link_comps
        drop constraint asset_link_comps_project_id_target_block_key_fkey;
    `);
    await queryRunner.query(`
      INSERT INTO asset_block_comps(project_id, block_key, asset_id, dirty_at)
      SELECT project_id, block_key, asset_id, NOW()
      FROM asset_blocks
      WHERE project_id = 1
      ON CONFLICT DO NOTHING
  `);
    await queryRunner.query(`
        alter table assets
        drop column readiness_rate;
    `);
    await queryRunner.query(`
        alter table assets
        drop column readiness_dirty_at;
    `);
    await queryRunner.query(`
        alter table assets
        drop column readiness_updated_at;
    `);
    await queryRunner.query(`
        create table if not exists public.asset_comps
        (
            project_id BIGINT NOT NULL REFERENCES projects(id) ON UPDATE CASCADE ON DELETE CASCADE,
            id UUID not null,
            type_ids uuid[],
            icon varchar,
            readiness_rate float,
            dirty_at timestamptz,
            computed_at timestamptz,
            PRIMARY KEY (project_id, id),
            foreign key (project_id, id) references assets(project_id, id) ON UPDATE CASCADE ON DELETE CASCADE
        );
    `);
    await queryRunner.query(`
        CREATE INDEX asset_comps_if_dirty
        ON asset_comps(project_id, id)
        WHERE dirty_at > computed_at OR computed_at IS NULL;
    `);
    await queryRunner.query(`
      INSERT INTO asset_comps(project_id, id, dirty_at)
      SELECT project_id, id, NOW()
      FROM assets
  `);
    await queryRunner.query(`
        ALTER TYPE asset_link_type ADD VALUE 'task';
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
        DROP TABLE public.asset_comps;
    `);
    await queryRunner.query(`
        alter table asset_blocks
        add readiness_scale float;
    `);
    await queryRunner.query(`
        alter table asset_blocks
        add deleted_at timestamptz
    `);
    await queryRunner.query(`
        alter table asset_blocks
        add readiness_rate double
    `);
    await queryRunner.query(`
        alter table asset_blocks
        add readiness_dirty_at timestamptz
    `);
    await queryRunner.query(`
        alter table asset_blocks
        add readiness_updated_at timestamptz
    `);
    await queryRunner.query(`
        ALTER TABLE asset_link_comps 
        ADD CONSTRAINT asset_link_comps_project_id_asset_id_fkey FOREIGN KEY (project_id, asset_id)
        references assets(project_id, id)  ON UPDATE CASCADE ON DELETE CASCADE;
        ALTER TABLE asset_link_comps 
        ADD CONSTRAINT asset_link_comps_project_id_target_asset_id_fkey FOREIGN KEY (project_id, target_asset_id)
        references assets(project_id, id)  ON UPDATE CASCADE ON DELETE CASCADE;
        ALTER TABLE asset_link_comps 
        ADD CONSTRAINT asset_link_comps_project_id_block_key_fkey FOREIGN KEY (project_id, target_block_key)
        references asset_block_keys(project_id, block_key)  ON UPDATE CASCADE ON DELETE CASCADE;
        ALTER TABLE asset_link_comps 
        ADD CONSTRAINT asset_link_comps_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;
    `);
  }
}
