-- CreateTable
CREATE TABLE `whitelistForm` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `description` TEXT NULL,
    `welcomeMessage` TEXT NULL,
    `submitButtonText` VARCHAR(191) NOT NULL DEFAULT 'Submit Application',
    `successMessage` TEXT NULL,
    `linkedBotId` INTEGER NULL,
    `fields` LONGTEXT NOT NULL,
    `status` VARCHAR(191) NOT NULL DEFAULT 'Active',
    `submissions` INTEGER NOT NULL DEFAULT 0,
    `createdBy` INTEGER NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `idx_form_created`(`createdAt`),
    INDEX `idx_form_status`(`status`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `formSubmission` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `formId` INTEGER NOT NULL,
    `submittedBy` VARCHAR(191) NOT NULL,
    `formData` LONGTEXT NOT NULL,
    `status` VARCHAR(191) NOT NULL DEFAULT 'Pending',
    `reviewedBy` INTEGER NULL,
    `reviewedAt` DATETIME(3) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `idx_submission_form`(`formId`),
    INDEX `idx_submission_created`(`createdAt`),
    INDEX `idx_submission_status`(`status`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `whitelistForm` ADD CONSTRAINT `whitelistForm_createdBy_fkey` FOREIGN KEY (`createdBy`) REFERENCES `user`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `formSubmission` ADD CONSTRAINT `formSubmission_formId_fkey` FOREIGN KEY (`formId`) REFERENCES `whitelistForm`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `formSubmission` ADD CONSTRAINT `formSubmission_reviewedBy_fkey` FOREIGN KEY (`reviewedBy`) REFERENCES `user`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
