-- CreateTable: chat_rooms
CREATE TABLE "rebreak"."chat_rooms" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "name" TEXT NOT NULL,
    "description" TEXT,
    "is_public" BOOLEAN NOT NULL DEFAULT false,
    "avatar_url" TEXT,
    "created_by" UUID NOT NULL,
    "join_mode" TEXT NOT NULL DEFAULT 'open',
    "invite_code" TEXT,
    "member_count" INTEGER NOT NULL DEFAULT 0,
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ NOT NULL,

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

-- CreateIndex: chat_rooms.invite_code
CREATE UNIQUE INDEX "chat_rooms_invite_code_key" ON "rebreak"."chat_rooms"("invite_code");

-- CreateTable: chat_room_members
CREATE TABLE "rebreak"."chat_room_members" (
    "room_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "role" TEXT NOT NULL DEFAULT 'member',
    "status" TEXT NOT NULL DEFAULT 'active',
    "joined_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "chat_room_members_pkey" PRIMARY KEY ("room_id","user_id")
);

-- AlterTable: chat_messages – neue Spalten
ALTER TABLE "rebreak"."chat_messages" ADD COLUMN "reply_to_id" UUID;
ALTER TABLE "rebreak"."chat_messages" ADD COLUMN "attachment_url" TEXT;
ALTER TABLE "rebreak"."chat_messages" ADD COLUMN "attachment_type" TEXT;
ALTER TABLE "rebreak"."chat_messages" ADD COLUMN "attachment_name" TEXT;
ALTER TABLE "rebreak"."chat_messages" ADD COLUMN "likes_count" INTEGER NOT NULL DEFAULT 0;

-- CreateTable: chat_message_likes
CREATE TABLE "rebreak"."chat_message_likes" (
    "user_id" UUID NOT NULL,
    "message_id" UUID NOT NULL,

    CONSTRAINT "chat_message_likes_pkey" PRIMARY KEY ("user_id","message_id")
);

-- AlterTable: direct_messages – neue Spalten
ALTER TABLE "rebreak"."direct_messages" ADD COLUMN "reply_to_id" UUID;
ALTER TABLE "rebreak"."direct_messages" ADD COLUMN "attachment_url" TEXT;
ALTER TABLE "rebreak"."direct_messages" ADD COLUMN "attachment_type" TEXT;
ALTER TABLE "rebreak"."direct_messages" ADD COLUMN "attachment_name" TEXT;
ALTER TABLE "rebreak"."direct_messages" ADD COLUMN "likes_count" INTEGER NOT NULL DEFAULT 0;

-- CreateTable: direct_message_likes
CREATE TABLE "rebreak"."direct_message_likes" (
    "user_id" UUID NOT NULL,
    "message_id" UUID NOT NULL,

    CONSTRAINT "direct_message_likes_pkey" PRIMARY KEY ("user_id","message_id")
);

-- AddForeignKey: chat_room_members -> chat_rooms
ALTER TABLE "rebreak"."chat_room_members" ADD CONSTRAINT "chat_room_members_room_id_fkey" FOREIGN KEY ("room_id") REFERENCES "rebreak"."chat_rooms"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: chat_messages -> chat_rooms
ALTER TABLE "rebreak"."chat_messages" ADD CONSTRAINT "chat_messages_room_id_fkey" FOREIGN KEY ("room_id") REFERENCES "rebreak"."chat_rooms"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: chat_messages self-reply
ALTER TABLE "rebreak"."chat_messages" ADD CONSTRAINT "chat_messages_reply_to_id_fkey" FOREIGN KEY ("reply_to_id") REFERENCES "rebreak"."chat_messages"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey: chat_message_likes -> chat_messages
ALTER TABLE "rebreak"."chat_message_likes" ADD CONSTRAINT "chat_message_likes_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "rebreak"."chat_messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: direct_messages self-reply
ALTER TABLE "rebreak"."direct_messages" ADD CONSTRAINT "direct_messages_reply_to_id_fkey" FOREIGN KEY ("reply_to_id") REFERENCES "rebreak"."direct_messages"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey: direct_message_likes -> direct_messages
ALTER TABLE "rebreak"."direct_message_likes" ADD CONSTRAINT "direct_message_likes_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "rebreak"."direct_messages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
