import { MigrationInterface, QueryRunner } from "typeorm"

export class CreateLeaveRequestTable1762499408508 implements MigrationInterface {
  name = "CreateLeaveRequestTable1762499408508"

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TYPE "public"."leave_requests_status_enum" AS ENUM('pending', 'approved', 'rejected', 'cancelled')`,
    )
    await queryRunner.query(
      `CREATE TABLE "leave_requests" ("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, "leave_type_id" integer NOT NULL, "from_date" date NOT NULL, "to_date" date NOT NULL, "duration_days" numeric(5,2) NOT NULL, "reason" text NOT NULL, "status" "public"."leave_requests_status_enum" NOT NULL DEFAULT 'pending', "approved_by" integer, "approved_at" TIMESTAMP, "comments" text, CONSTRAINT "PK_d3abcf9a16cef1450129e06fa9f" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `CREATE TABLE "employee_leave_balances" ("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, "leave_type_id" integer NOT NULL, "total_allocated" integer NOT NULL, "used_days" integer NOT NULL DEFAULT '0', "available_days" integer NOT NULL, "year" integer NOT NULL, CONSTRAINT "PK_79b46835dd12470c6658bf5ea76" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `ALTER TABLE "leave_requests" ADD CONSTRAINT "FK_43c15abe870cf2a725b3d722b78" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "leave_requests" ADD CONSTRAINT "FK_52b4b7c7d295e204add6dbe0a09" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "leave_requests" ADD CONSTRAINT "FK_54a57db316598806786c2b95323" FOREIGN KEY ("leave_type_id") REFERENCES "leave_types"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "leave_requests" ADD CONSTRAINT "FK_fe3e6c3fea2c56aaaad8cedbc20" FOREIGN KEY ("approved_by") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_leave_balances" ADD CONSTRAINT "FK_c3880862e56da6558fc4000a21f" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_leave_balances" ADD CONSTRAINT "FK_ae0ef642b252a67d79108470086" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_leave_balances" ADD CONSTRAINT "FK_f6fa413bdab8eb425456c026b8c" FOREIGN KEY ("leave_type_id") REFERENCES "leave_types"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "employee_leave_balances" DROP CONSTRAINT "FK_f6fa413bdab8eb425456c026b8c"`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_leave_balances" DROP CONSTRAINT "FK_ae0ef642b252a67d79108470086"`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_leave_balances" DROP CONSTRAINT "FK_c3880862e56da6558fc4000a21f"`,
    )
    await queryRunner.query(
      `ALTER TABLE "leave_requests" DROP CONSTRAINT "FK_fe3e6c3fea2c56aaaad8cedbc20"`,
    )
    await queryRunner.query(
      `ALTER TABLE "leave_requests" DROP CONSTRAINT "FK_54a57db316598806786c2b95323"`,
    )
    await queryRunner.query(
      `ALTER TABLE "leave_requests" DROP CONSTRAINT "FK_52b4b7c7d295e204add6dbe0a09"`,
    )
    await queryRunner.query(
      `ALTER TABLE "leave_requests" DROP CONSTRAINT "FK_43c15abe870cf2a725b3d722b78"`,
    )
    await queryRunner.query(`DROP TABLE "employee_leave_balances"`)
    await queryRunner.query(`DROP TABLE "leave_requests"`)
    await queryRunner.query(`DROP TYPE "public"."leave_requests_status_enum"`)
  }
}
