-- AlterTable
ALTER TABLE `board` ADD COLUMN `portalEnabled` BOOLEAN NOT NULL DEFAULT false,
    ADD COLUMN `portalIntakeListId` VARCHAR(191) NULL,
    ADD COLUMN `portalSlug` VARCHAR(191) NULL,
    ADD COLUMN `portalTitle` VARCHAR(191) NULL,
    ADD COLUMN `portalWelcomeMessage` TEXT NULL;

-- AlterTable
ALTER TABLE `boardmember` ADD COLUMN `isPortalRequester` BOOLEAN NOT NULL DEFAULT false;

-- AlterTable
ALTER TABLE `card` ADD COLUMN `portalRequestTypeId` VARCHAR(191) NULL,
    ADD COLUMN `submitterEmail` VARCHAR(191) NULL,
    ADD COLUMN `submitterId` VARCHAR(191) NULL,
    ADD COLUMN `submitterName` VARCHAR(191) NULL;

-- AlterTable
ALTER TABLE `notification` MODIFY `type` ENUM('MENTION', 'ASSIGNMENT', 'DUE_DATE', 'COMMENT', 'BOARD_INVITE', 'DRIVE_SHARE', 'PORTAL_REQUEST') NOT NULL;

-- AlterTable
ALTER TABLE `notificationpreference` ADD COLUMN `emailOnPortalRequest` BOOLEAN NOT NULL DEFAULT true;

-- CreateTable
CREATE TABLE `PortalCategory` (
    `id` VARCHAR(191) NOT NULL,
    `boardId` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `icon` VARCHAR(191) NOT NULL DEFAULT 'Layers',
    `position` DOUBLE NOT NULL,

    INDEX `PortalCategory_boardId_idx`(`boardId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `PortalRequestType` (
    `id` VARCHAR(191) NOT NULL,
    `categoryId` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,
    `icon` VARCHAR(191) NOT NULL DEFAULT 'FileText',
    `instructions` TEXT NULL,
    `position` DOUBLE NOT NULL,

    INDEX `PortalRequestType_categoryId_idx`(`categoryId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `PortalField` (
    `id` VARCHAR(191) NOT NULL,
    `requestTypeId` VARCHAR(191) NOT NULL,
    `label` VARCHAR(191) NOT NULL,
    `fieldType` ENUM('TEXT', 'TEXTAREA', 'EMAIL', 'ATTACHMENT') NOT NULL,
    `required` BOOLEAN NOT NULL DEFAULT false,
    `position` DOUBLE NOT NULL,

    INDEX `PortalField_requestTypeId_idx`(`requestTypeId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `PortalAnswer` (
    `id` VARCHAR(191) NOT NULL,
    `cardId` VARCHAR(191) NOT NULL,
    `fieldId` VARCHAR(191) NULL,
    `label` VARCHAR(191) NOT NULL,
    `value` TEXT NOT NULL,

    INDEX `PortalAnswer_cardId_idx`(`cardId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateIndex
CREATE UNIQUE INDEX `Board_portalSlug_key` ON `Board`(`portalSlug`);

-- CreateIndex
CREATE INDEX `Card_submitterId_idx` ON `Card`(`submitterId`);

-- AddForeignKey
ALTER TABLE `Card` ADD CONSTRAINT `Card_submitterId_fkey` FOREIGN KEY (`submitterId`) REFERENCES `User`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Card` ADD CONSTRAINT `Card_portalRequestTypeId_fkey` FOREIGN KEY (`portalRequestTypeId`) REFERENCES `PortalRequestType`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `PortalCategory` ADD CONSTRAINT `PortalCategory_boardId_fkey` FOREIGN KEY (`boardId`) REFERENCES `Board`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `PortalRequestType` ADD CONSTRAINT `PortalRequestType_categoryId_fkey` FOREIGN KEY (`categoryId`) REFERENCES `PortalCategory`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `PortalField` ADD CONSTRAINT `PortalField_requestTypeId_fkey` FOREIGN KEY (`requestTypeId`) REFERENCES `PortalRequestType`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `PortalAnswer` ADD CONSTRAINT `PortalAnswer_cardId_fkey` FOREIGN KEY (`cardId`) REFERENCES `Card`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `PortalAnswer` ADD CONSTRAINT `PortalAnswer_fieldId_fkey` FOREIGN KEY (`fieldId`) REFERENCES `PortalField`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
