-- CreateTable
CREATE TABLE `article_share_requests` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `reason` TEXT NULL,
    `status` VARCHAR(20) NOT NULL DEFAULT 'pending',
    `is_deleted` BOOLEAN NOT NULL DEFAULT false,
    `article_id` INTEGER NOT NULL,
    `requester_id` INTEGER NOT NULL,
    `reviewer_id` INTEGER NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,
    `reviewed_at` DATETIME(3) NULL,

    INDEX `article_share_requests_status_idx`(`status`),
    INDEX `article_share_requests_article_id_idx`(`article_id`),
    INDEX `article_share_requests_requester_id_idx`(`requester_id`),
    UNIQUE INDEX `article_share_requests_article_id_requester_id_key`(`article_id`, `requester_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `article_share_permissions` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `shareToken` VARCHAR(255) NOT NULL,
    `is_permanent` BOOLEAN NOT NULL DEFAULT false,
    `expires_at` DATETIME(3) NULL,
    `is_active` BOOLEAN NOT NULL DEFAULT true,
    `access_count` INTEGER NOT NULL DEFAULT 0,
    `max_access` INTEGER NULL,
    `is_deleted` BOOLEAN NOT NULL DEFAULT false,
    `article_id` INTEGER NOT NULL,
    `user_id` INTEGER NOT NULL,
    `granted_by_id` INTEGER NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    UNIQUE INDEX `article_share_permissions_shareToken_key`(`shareToken`),
    INDEX `article_share_permissions_shareToken_idx`(`shareToken`),
    INDEX `article_share_permissions_user_id_idx`(`user_id`),
    INDEX `article_share_permissions_article_id_idx`(`article_id`),
    INDEX `article_share_permissions_expires_at_idx`(`expires_at`),
    UNIQUE INDEX `article_share_permissions_article_id_user_id_key`(`article_id`, `user_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `article_share_requests` ADD CONSTRAINT `article_share_requests_article_id_fkey` FOREIGN KEY (`article_id`) REFERENCES `articles`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `article_share_requests` ADD CONSTRAINT `article_share_requests_requester_id_fkey` FOREIGN KEY (`requester_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `article_share_requests` ADD CONSTRAINT `article_share_requests_reviewer_id_fkey` FOREIGN KEY (`reviewer_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `article_share_permissions` ADD CONSTRAINT `article_share_permissions_article_id_fkey` FOREIGN KEY (`article_id`) REFERENCES `articles`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `article_share_permissions` ADD CONSTRAINT `article_share_permissions_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `article_share_permissions` ADD CONSTRAINT `article_share_permissions_granted_by_id_fkey` FOREIGN KEY (`granted_by_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
