import { MigrationInterface, QueryRunner } from "typeorm"

export class AddInspectionReportEntity1753441069962 implements MigrationInterface {
  name = "AddInspectionReportEntity1753441069962"

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE "inspection_fleet_answers" ("id" SERIAL NOT NULL, "inspection_fleet_report_id" integer NOT NULL, "question_id" integer NOT NULL, "selected_answer_id" integer NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), "deleted_at" TIMESTAMP, CONSTRAINT "PK_275e4f0613e81e2a7943c76c15b" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `CREATE TABLE "inspection_fleet_reports" ("id" SERIAL NOT NULL, "fleet_id" integer, "driver_id" integer, "front_side_photo" character varying, "right_side_photo" character varying, "back_side_photo" character varying, "left_side_photo" character varying, "created_at" TIMESTAMP NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(), "deleted_at" TIMESTAMP, CONSTRAINT "PK_80731f2d660491fcca722f88ed7" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_answers" ADD CONSTRAINT "FK_2aa09e9f4c516a719daed92732a" FOREIGN KEY ("inspection_fleet_report_id") REFERENCES "inspection_fleet_reports"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_answers" ADD CONSTRAINT "FK_bbd371764ce252a5f867ca5f5bc" FOREIGN KEY ("question_id") REFERENCES "inspection_questions"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_answers" ADD CONSTRAINT "FK_1641a936f0cffb8196a12bdd556" FOREIGN KEY ("selected_answer_id") REFERENCES "inspection_answer_options"("id") ON DELETE SET NULL ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_reports" ADD CONSTRAINT "FK_044cc5d4b1b35f036580a8071e9" FOREIGN KEY ("driver_id") REFERENCES "team_members"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_reports" ADD CONSTRAINT "FK_e3a05785eef79a300a6a03c4df5" FOREIGN KEY ("fleet_id") REFERENCES "fleet_operations"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
    )
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_reports" DROP CONSTRAINT "FK_e3a05785eef79a300a6a03c4df5"`,
    )
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_reports" DROP CONSTRAINT "FK_044cc5d4b1b35f036580a8071e9"`,
    )
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_answers" DROP CONSTRAINT "FK_1641a936f0cffb8196a12bdd556"`,
    )
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_answers" DROP CONSTRAINT "FK_bbd371764ce252a5f867ca5f5bc"`,
    )
    await queryRunner.query(
      `ALTER TABLE "inspection_fleet_answers" DROP CONSTRAINT "FK_2aa09e9f4c516a719daed92732a"`,
    )
    await queryRunner.query(`DROP TABLE "inspection_fleet_reports"`)
    await queryRunner.query(`DROP TABLE "inspection_fleet_answers"`)
  }
}
