import { MigrationInterface, QueryRunner } from "typeorm"

export class CreateClockInRecordsTable1770182352116 implements MigrationInterface {
  name = "CreateClockInRecordsTable1770182352116"

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE "clock_in_records" ("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, "clock_in" TIMESTAMP NOT NULL, "clock_out" TIMESTAMP, "duration" integer, CONSTRAINT "PK_5a724c4918b143174e8d938b1f7" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_leave_balances" ALTER COLUMN "available_days" DROP DEFAULT`,
    )
    await queryRunner.query(
      `ALTER TABLE "clock_in_records" ADD CONSTRAINT "FK_34e544a83aa4309a27c6f7db0eb" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "clock_in_records" ADD CONSTRAINT "FK_0361df3fcc7ce94c369e04d62ed" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "clock_in_records" DROP CONSTRAINT "FK_0361df3fcc7ce94c369e04d62ed"`,
    )
    await queryRunner.query(
      `ALTER TABLE "clock_in_records" DROP CONSTRAINT "FK_34e544a83aa4309a27c6f7db0eb"`,
    )
    await queryRunner.query(
      `ALTER TABLE "employee_leave_balances" ALTER COLUMN "available_days" SET DEFAULT '0'`,
    )
    await queryRunner.query(`DROP TABLE "clock_in_records"`)
  }
}
