import { MigrationInterface, QueryRunner } from "typeorm"

export class AddEmployeeFlagsTable1771776210300 implements MigrationInterface {
  name = "AddEmployeeFlagsTable1771776210300"

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE "flags" ("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', CONSTRAINT "PK_ea7e333c92a55de9e9b8d0b9afd" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `CREATE TABLE "employee_flags" ("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, "company_id" integer NOT NULL, "employee_id" integer NOT NULL, "flag_id" integer NOT NULL, "action_by_id" integer NOT NULL, "reason" text, "resolve_at" date NOT NULL, "status" character varying NOT NULL DEFAULT 'pending', CONSTRAINT "PK_41bf738c3f7fc8f946f3dc16de5" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_flags" ADD CONSTRAINT "FK_10a65bcce26213690fed62653e0" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_flags" ADD CONSTRAINT "FK_022cce6dfadeeafc2bf8db9c02d" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_flags" ADD CONSTRAINT "FK_8e63fc1959a876d2a977c8f4a32" FOREIGN KEY ("flag_id") REFERENCES "flags"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_flags" ADD CONSTRAINT "FK_f041ed19d312c92d367adc5aaa8" FOREIGN KEY ("action_by_id") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "employee_flags" DROP CONSTRAINT "FK_f041ed19d312c92d367adc5aaa8"`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_flags" DROP CONSTRAINT "FK_8e63fc1959a876d2a977c8f4a32"`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_flags" DROP CONSTRAINT "FK_022cce6dfadeeafc2bf8db9c02d"`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_flags" DROP CONSTRAINT "FK_10a65bcce26213690fed62653e0"`,
    )
    await queryRunner.query(`DROP TABLE "employee_flags"`)
    await queryRunner.query(`DROP TABLE "flags"`)
  }
}
