-- AlterTable: UploadedFile
ALTER TABLE `UploadedFile` ADD COLUMN `sha256` VARCHAR(64) NULL,
    ADD COLUMN `purpose` VARCHAR(32) NULL;

CREATE INDEX `UploadedFile_sha256_idx` ON `UploadedFile`(`sha256`);

-- AlterTable: DocumentImport
ALTER TABLE `DocumentImport` ADD COLUMN `sourceId` VARCHAR(191) NULL,
    ADD COLUMN `step` VARCHAR(32) NULL,
    ADD COLUMN `workerId` VARCHAR(255) NULL,
    ADD COLUMN `retryCount` INT NOT NULL DEFAULT 0,
    ADD COLUMN `maxRetries` INT NOT NULL DEFAULT 3,
    ADD COLUMN `heartbeatAt` DATETIME(3) NULL,
    ADD COLUMN `errorCode` VARCHAR(32) NULL,
    MODIFY `status` VARCHAR(32) NOT NULL DEFAULT 'QUEUED';

CREATE INDEX `DocumentImport_sourceId_idx` ON `DocumentImport`(`sourceId`);
CREATE INDEX `DocumentImport_workerId_idx` ON `DocumentImport`(`workerId`);

-- AlterTable: KnowledgeItem
ALTER TABLE `KnowledgeItem` ADD COLUMN `sourceDeleted` BOOLEAN NOT NULL DEFAULT false,
    ADD COLUMN `sourceTitleSnapshot` VARCHAR(255) NULL,
    ADD COLUMN `sourceSnippetSnapshot` TEXT NULL;

-- CreateTable: KnowledgeSource
CREATE TABLE `KnowledgeSource` (
    `id` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `knowledgeBaseId` VARCHAR(191) NOT NULL,
    `fileId` VARCHAR(191) NULL,
    `type` VARCHAR(32) NOT NULL DEFAULT 'file',
    `title` VARCHAR(255) NULL,
    `originalFilename` VARCHAR(255) NULL,
    `mimeType` VARCHAR(100) NULL,
    `sizeBytes` BIGINT NOT NULL DEFAULT 0,
    `textLength` INT NOT NULL DEFAULT 0,
    `parseStatus` VARCHAR(32) NOT NULL DEFAULT 'pending',
    `indexStatus` VARCHAR(32) NOT NULL DEFAULT 'pending',
    `learningStatus` VARCHAR(32) NOT NULL DEFAULT 'pending',
    `parsedObjectKey` VARCHAR(500) NULL,
    `metadataObjectKey` VARCHAR(500) NULL,
    `originalObjectKey` VARCHAR(500) NULL,
    `version` INT NOT NULL DEFAULT 1,
    `parentSourceId` VARCHAR(191) NULL,
    `replacedBySourceId` VARCHAR(191) NULL,
    `errorCode` VARCHAR(32) NULL,
    `errorMessage` TEXT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `deletedAt` DATETIME(3) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE INDEX `KnowledgeSource_userId_idx` ON `KnowledgeSource`(`userId`);
CREATE INDEX `KnowledgeSource_knowledgeBaseId_idx` ON `KnowledgeSource`(`knowledgeBaseId`);
CREATE INDEX `KnowledgeSource_fileId_idx` ON `KnowledgeSource`(`fileId`);
CREATE INDEX `KnowledgeSource_parseStatus_idx` ON `KnowledgeSource`(`parseStatus`);
CREATE INDEX `KnowledgeSource_indexStatus_idx` ON `KnowledgeSource`(`indexStatus`);

-- CreateTable: KnowledgeChunk
CREATE TABLE `KnowledgeChunk` (
    `id` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `knowledgeBaseId` VARCHAR(191) NOT NULL,
    `sourceId` VARCHAR(191) NOT NULL,
    `content` LONGTEXT NOT NULL,
    `chunkIndex` INT NOT NULL,
    `pageNumber` INT NULL,
    `sectionTitle` VARCHAR(500) NULL,
    `tokenCount` INT NOT NULL DEFAULT 0,
    `externalVectorId` VARCHAR(255) NULL,
    `embeddingModel` VARCHAR(100) NULL,
    `embeddingStatus` VARCHAR(32) NOT NULL DEFAULT 'pending',
    `metadataJson` JSON NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `deletedAt` DATETIME(3) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE INDEX `KnowledgeChunk_userId_idx` ON `KnowledgeChunk`(`userId`);
CREATE INDEX `KnowledgeChunk_sourceId_idx` ON `KnowledgeChunk`(`sourceId`);
CREATE INDEX `KnowledgeChunk_knowledgeBaseId_idx` ON `KnowledgeChunk`(`knowledgeBaseId`);
CREATE INDEX `KnowledgeChunk_externalVectorId_idx` ON `KnowledgeChunk`(`externalVectorId`);

-- CreateTable: ImportCandidate
CREATE TABLE `ImportCandidate` (
    `id` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `knowledgeBaseId` VARCHAR(191) NOT NULL,
    `sourceId` VARCHAR(191) NOT NULL,
    `importId` VARCHAR(191) NOT NULL,
    `title` VARCHAR(255) NOT NULL,
    `summary` TEXT NULL,
    `content` LONGTEXT NULL,
    `tagsJson` JSON NULL,
    `recallQuestionsJson` JSON NULL,
    `sourceTextSnippet` TEXT NULL,
    `sourceChunkIds` JSON NULL,
    `confidence` DECIMAL(4, 3) NOT NULL DEFAULT 0,
    `difficulty` VARCHAR(16) NULL,
    `orderIndex` INT NOT NULL DEFAULT 0,
    `status` VARCHAR(16) NOT NULL DEFAULT 'PENDING',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE INDEX `ImportCandidate_userId_idx` ON `ImportCandidate`(`userId`);
CREATE INDEX `ImportCandidate_sourceId_idx` ON `ImportCandidate`(`sourceId`);
CREATE INDEX `ImportCandidate_importId_idx` ON `ImportCandidate`(`importId`);
CREATE INDEX `ImportCandidate_status_idx` ON `ImportCandidate`(`status`);

-- CreateTable: BackupJob
CREATE TABLE `BackupJob` (
    `id` VARCHAR(191) NOT NULL,
    `type` VARCHAR(16) NOT NULL,
    `status` VARCHAR(16) NOT NULL DEFAULT 'RUNNING',
    `localPath` VARCHAR(500) NULL,
    `cosObjectKey` VARCHAR(500) NULL,
    `fileSizeBytes` BIGINT NOT NULL DEFAULT 0,
    `startedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `completedAt` DATETIME(3) NULL,
    `errorMessage` TEXT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable: MembershipPlan
CREATE TABLE `MembershipPlan` (
    `id` VARCHAR(191) NOT NULL,
    `code` VARCHAR(32) NOT NULL,
    `name` VARCHAR(100) NOT NULL,
    `priceMonthly` INT NOT NULL DEFAULT 0,
    `priceYearly` INT NOT NULL DEFAULT 0,
    `maxKnowledgeBases` INT NOT NULL DEFAULT 1,
    `maxStorageBytes` BIGINT NOT NULL DEFAULT 0,
    `maxFileSizeBytes` BIGINT NOT NULL DEFAULT 0,
    `monthlyOcrPages` INT NOT NULL DEFAULT 0,
    `monthlyVisionPages` INT NOT NULL DEFAULT 0,
    `monthlyChatCount` INT NOT NULL DEFAULT 0,
    `monthlyAiAnalysisCount` INT NOT NULL DEFAULT 0,
    `monthlyRecallCount` INT NOT NULL DEFAULT 0,
    `monthlyCardGenCount` INT NOT NULL DEFAULT 0,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    UNIQUE INDEX `MembershipPlan_code_key`(`code`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `KnowledgeSource` ADD CONSTRAINT `KnowledgeSource_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `KnowledgeSource` ADD CONSTRAINT `KnowledgeSource_knowledgeBaseId_fkey` FOREIGN KEY (`knowledgeBaseId`) REFERENCES `KnowledgeBase`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `KnowledgeSource` ADD CONSTRAINT `KnowledgeSource_fileId_fkey` FOREIGN KEY (`fileId`) REFERENCES `UploadedFile`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE `KnowledgeChunk` ADD CONSTRAINT `KnowledgeChunk_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `KnowledgeChunk` ADD CONSTRAINT `KnowledgeChunk_knowledgeBaseId_fkey` FOREIGN KEY (`knowledgeBaseId`) REFERENCES `KnowledgeBase`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `KnowledgeChunk` ADD CONSTRAINT `KnowledgeChunk_sourceId_fkey` FOREIGN KEY (`sourceId`) REFERENCES `KnowledgeSource`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE `ImportCandidate` ADD CONSTRAINT `ImportCandidate_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `ImportCandidate` ADD CONSTRAINT `ImportCandidate_knowledgeBaseId_fkey` FOREIGN KEY (`knowledgeBaseId`) REFERENCES `KnowledgeBase`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `ImportCandidate` ADD CONSTRAINT `ImportCandidate_sourceId_fkey` FOREIGN KEY (`sourceId`) REFERENCES `KnowledgeSource`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `ImportCandidate` ADD CONSTRAINT `ImportCandidate_importId_fkey` FOREIGN KEY (`importId`) REFERENCES `DocumentImport`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

ALTER TABLE `DocumentImport` ADD CONSTRAINT `DocumentImport_sourceId_fkey` FOREIGN KEY (`sourceId`) REFERENCES `KnowledgeSource`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
