-- CreateTable CoursePlaylist
CREATE TABLE "CoursePlaylist" (
    "id" TEXT NOT NULL,
    "courseId" TEXT NOT NULL,
    "playlistId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex for CoursePlaylist
CREATE UNIQUE INDEX "CoursePlaylist_courseId_playlistId_key" ON "CoursePlaylist"("courseId", "playlistId");
CREATE INDEX "CoursePlaylist_courseId_idx" ON "CoursePlaylist"("courseId");
CREATE INDEX "CoursePlaylist_playlistId_idx" ON "CoursePlaylist"("playlistId");

-- Add foreign key constraints
ALTER TABLE "CoursePlaylist" ADD CONSTRAINT "CoursePlaylist_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "CoursePlaylist" ADD CONSTRAINT "CoursePlaylist_playlistId_fkey" FOREIGN KEY ("playlistId") REFERENCES "Playlist"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- Update Playlist table to add ON DELETE CASCADE to courseId
ALTER TABLE "Playlist" DROP CONSTRAINT IF EXISTS "Playlist_courseId_fkey";
ALTER TABLE "Playlist" ADD CONSTRAINT "Playlist_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE CASCADE ON UPDATE CASCADE;
