import { MigrationInterface, QueryRunner } from "typeorm"

export class ChatEntity1759995470855 implements MigrationInterface {
  name = "ChatEntity1759995470855"

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE "chat_messages" ("id" SERIAL NOT NULL, "chat_room_id" integer NOT NULL, "sender_type" character varying NOT NULL, "sender_id" integer NOT NULL, "receiver_type" character varying NOT NULL, "receiver_id" integer NOT NULL, "message_text" text NOT NULL, "is_read" boolean NOT NULL DEFAULT false, "created_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_40c55ee0e571e268b0d3cd37d10" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `CREATE TABLE "chat_participants" ("id" SERIAL NOT NULL, "chat_room_id" integer NOT NULL, "participant_type" character varying NOT NULL, "participant_id" integer NOT NULL, "joined_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_ebf68c52a2b4dceb777672b782d" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `CREATE INDEX "idx_chat_participant_room" ON "chat_participants" ("chat_room_id") `,
    )
    await queryRunner.query(
      `CREATE TABLE "chat_rooms" ("id" SERIAL NOT NULL, "chat_type" character varying NOT NULL, "trip_id" integer, "created_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_c69082bd83bffeb71b0f455bd59" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `CREATE TABLE "chat_conversations" ("id" SERIAL NOT NULL, "dispatcher_id" integer NOT NULL, "driver_id" integer NOT NULL, "dispatcher_unread_count" integer NOT NULL DEFAULT '0', "driver_unread_count" integer NOT NULL DEFAULT '0', "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_ff117d9f57807c4f2e3034a39f3" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `ALTER TABLE "chat_messages" ADD CONSTRAINT "FK_3217ec6230770d4d2c826fc0380" FOREIGN KEY ("chat_room_id") REFERENCES "chat_rooms"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "chat_participants" ADD CONSTRAINT "FK_d04f6115f51cd8fd8551320dd02" FOREIGN KEY ("chat_room_id") REFERENCES "chat_rooms"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "chat_conversations" ADD CONSTRAINT "FK_dab1b14046533d3a507a31d01b9" FOREIGN KEY ("dispatcher_id") REFERENCES "team_members"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "chat_conversations" ADD CONSTRAINT "FK_8df8006cdab6f717abb071446d0" FOREIGN KEY ("driver_id") REFERENCES "team_members"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "chat_conversations" DROP CONSTRAINT "FK_8df8006cdab6f717abb071446d0"`,
    )
    await queryRunner.query(
      `ALTER TABLE "chat_conversations" DROP CONSTRAINT "FK_dab1b14046533d3a507a31d01b9"`,
    )
    await queryRunner.query(
      `ALTER TABLE "chat_participants" DROP CONSTRAINT "FK_d04f6115f51cd8fd8551320dd02"`,
    )
    await queryRunner.query(
      `ALTER TABLE "chat_messages" DROP CONSTRAINT "FK_3217ec6230770d4d2c826fc0380"`,
    )
    await queryRunner.query(`DROP TABLE "chat_conversations"`)
    await queryRunner.query(`DROP TABLE "chat_rooms"`)
    await queryRunner.query(`DROP INDEX "public"."idx_chat_participant_room"`)
    await queryRunner.query(`DROP TABLE "chat_participants"`)
    await queryRunner.query(`DROP TABLE "chat_messages"`)
  }
}
