-- CreateEnum
CREATE TYPE "messageType" AS ENUM ('msg', 'file', 'image', 'video');

-- CreateTable
CREATE TABLE "tbl_chat_room" (
    "id" BIGSERIAL NOT NULL,
    "sender_id" BIGINT NOT NULL,
    "receiver_id" BIGINT NOT NULL,
    "last_message" TEXT,
    "last_message_at" TIMESTAMP(3),
    "is_active" BOOLEAN DEFAULT true,
    "is_delete" BOOLEAN DEFAULT false,
    "deleted_at" TIMESTAMP(3),
    "inserted_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

    CONSTRAINT "tbl_chat_room_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "tbl_chat" (
    "id" BIGSERIAL NOT NULL,
    "chat_room_id" BIGINT NOT NULL,
    "sender_id" BIGINT NOT NULL,
    "receiver_id" BIGINT NOT NULL,
    "message" TEXT,
    "message_type" "messageType",
    "is_read" BOOLEAN DEFAULT false,
    "is_active" BOOLEAN DEFAULT true,
    "is_delete" BOOLEAN DEFAULT false,
    "deleted_at" TIMESTAMP(3),
    "inserted_at" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

    CONSTRAINT "tbl_chat_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "tbl_chat_room_sender_id_idx" ON "tbl_chat_room"("sender_id");

-- CreateIndex
CREATE INDEX "tbl_chat_room_receiver_id_idx" ON "tbl_chat_room"("receiver_id");

-- CreateIndex
CREATE INDEX "tbl_chat_room_last_message_idx" ON "tbl_chat_room"("last_message");

-- CreateIndex
CREATE INDEX "tbl_chat_room_last_message_at_idx" ON "tbl_chat_room"("last_message_at");

-- CreateIndex
CREATE INDEX "tbl_chat_chat_room_id_idx" ON "tbl_chat"("chat_room_id");

-- CreateIndex
CREATE INDEX "tbl_chat_sender_id_idx" ON "tbl_chat"("sender_id");

-- CreateIndex
CREATE INDEX "tbl_chat_receiver_id_idx" ON "tbl_chat"("receiver_id");

-- CreateIndex
CREATE INDEX "tbl_chat_message_idx" ON "tbl_chat"("message");

-- CreateIndex
CREATE INDEX "tbl_chat_message_type_idx" ON "tbl_chat"("message_type");

-- CreateIndex
CREATE INDEX "tbl_chat_is_read_idx" ON "tbl_chat"("is_read");

-- AddForeignKey
ALTER TABLE "tbl_chat_room" ADD CONSTRAINT "tbl_chat_room_sender_id_fkey" FOREIGN KEY ("sender_id") REFERENCES "tbl_user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tbl_chat_room" ADD CONSTRAINT "tbl_chat_room_receiver_id_fkey" FOREIGN KEY ("receiver_id") REFERENCES "tbl_user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tbl_chat" ADD CONSTRAINT "tbl_chat_sender_id_fkey" FOREIGN KEY ("sender_id") REFERENCES "tbl_user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tbl_chat" ADD CONSTRAINT "tbl_chat_receiver_id_fkey" FOREIGN KEY ("receiver_id") REFERENCES "tbl_user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tbl_chat" ADD CONSTRAINT "tbl_chat_chat_room_id_fkey" FOREIGN KEY ("chat_room_id") REFERENCES "tbl_chat_room"("id") ON DELETE CASCADE ON UPDATE CASCADE;
