-- CreateTable
CREATE TABLE `MediaPost` (
    `id` VARCHAR(191) NOT NULL,
    `providerId` VARCHAR(191) NOT NULL,
    `bookingId` VARCHAR(191) NULL,
    `visibility` ENUM('public', 'private') NOT NULL DEFAULT 'public',
    `caption` TEXT NULL,
    `createdByUserId` VARCHAR(191) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `MediaPost_providerId_visibility_createdAt_idx`(`providerId`, `visibility`, `createdAt`),
    INDEX `MediaPost_bookingId_idx`(`bookingId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `MediaPostItem` (
    `id` VARCHAR(191) NOT NULL,
    `postId` VARCHAR(191) NOT NULL,
    `sortOrder` INTEGER NOT NULL DEFAULT 0,
    `thumbUrl` VARCHAR(191) NOT NULL,
    `fullUrl` VARCHAR(191) NOT NULL,
    `thumbStorageKey` VARCHAR(512) NOT NULL,
    `fullStorageKey` VARCHAR(512) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `MediaPostItem_thumbStorageKey_key`(`thumbStorageKey`),
    UNIQUE INDEX `MediaPostItem_fullStorageKey_key`(`fullStorageKey`),
    INDEX `MediaPostItem_postId_sortOrder_idx`(`postId`, `sortOrder`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `MediaPost` ADD CONSTRAINT `MediaPost_providerId_fkey` FOREIGN KEY (`providerId`) REFERENCES `Provider`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `MediaPost` ADD CONSTRAINT `MediaPost_bookingId_fkey` FOREIGN KEY (`bookingId`) REFERENCES `Booking`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `MediaPost` ADD CONSTRAINT `MediaPost_createdByUserId_fkey` FOREIGN KEY (`createdByUserId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `MediaPostItem` ADD CONSTRAINT `MediaPostItem_postId_fkey` FOREIGN KEY (`postId`) REFERENCES `MediaPost`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
