import { MigrationInterface, QueryRunner } from "typeorm"

export class AddMisBusinessKpisAndBusinessKpisDepartmentsTable1771956226984 implements MigrationInterface {
  name = "AddMisBusinessKpisAndBusinessKpisDepartmentsTable1771956226984"

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE "mis_business_kpis" ("created_by" integer, "updated_by" integer, "deleted_by" integer, "created_at" TIMESTAMP NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(), "deleted_at" TIMESTAMP, "id" SERIAL NOT NULL, "name" character varying NOT NULL, "status" smallint NOT NULL DEFAULT '1', "company_id" integer NOT NULL, CONSTRAINT "PK_5e824c5f544c26a2afdb77cc8d6" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `CREATE UNIQUE INDEX "IDX_9615ee1295585ac1e6e0a0e952" ON "mis_business_kpis" ("company_id", "name") `,
    )
    await queryRunner.query(
      `CREATE TABLE "business_kpis_departments" ("kpi_id" integer NOT NULL, "department_id" integer NOT NULL, CONSTRAINT "PK_7ba2a77fefaba09c8cc84afb1d6" PRIMARY KEY ("kpi_id", "department_id"))`,
    )
    await queryRunner.query(
      `CREATE INDEX "IDX_1e5c3cfb99198702e25df038f6" ON "business_kpis_departments" ("kpi_id") `,
    )
    await queryRunner.query(
      `CREATE INDEX "IDX_2350bb3a5dab63548d47254f8c" ON "business_kpis_departments" ("department_id") `,
    )
    await queryRunner.query(
      `ALTER TABLE "mis_business_kpis" ADD CONSTRAINT "FK_cd60f296a9c44d52f3cefff9876" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "business_kpis_departments" ADD CONSTRAINT "FK_1e5c3cfb99198702e25df038f6e" FOREIGN KEY ("kpi_id") REFERENCES "mis_business_kpis"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
    )
    await queryRunner.query(
      `ALTER TABLE "business_kpis_departments" ADD CONSTRAINT "FK_2350bb3a5dab63548d47254f8c5" FOREIGN KEY ("department_id") REFERENCES "departments"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "business_kpis_departments" DROP CONSTRAINT "FK_2350bb3a5dab63548d47254f8c5"`,
    )
    await queryRunner.query(
      `ALTER TABLE "business_kpis_departments" DROP CONSTRAINT "FK_1e5c3cfb99198702e25df038f6e"`,
    )
    await queryRunner.query(
      `ALTER TABLE "mis_business_kpis" DROP CONSTRAINT "FK_cd60f296a9c44d52f3cefff9876"`,
    )
    await queryRunner.query(`DROP TABLE "business_kpis_departments"`)
    await queryRunner.query(`DROP TABLE "mis_business_kpis"`)
  }
}
