-- AlterTable: UploadedFile add objectKey + bucket + index
ALTER TABLE `UploadedFile`
  ADD COLUMN `objectKey` VARCHAR(500) NULL AFTER `storagePath`,
  ADD COLUMN `bucket` VARCHAR(100) NULL AFTER `objectKey`;

CREATE INDEX `UploadedFile_objectKey_idx` ON `UploadedFile`(`objectKey`);

-- CreateTable: AiUsageLog
CREATE TABLE `AiUsageLog` (
    `id` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `feature` VARCHAR(64) NOT NULL,
    `provider` VARCHAR(32) NOT NULL,
    `model` VARCHAR(100) NOT NULL,
    `tier` VARCHAR(32) NOT NULL,
    `promptKey` VARCHAR(128) NOT NULL,
    `promptVersion` VARCHAR(32) NOT NULL,
    `inputTokens` INTEGER NOT NULL DEFAULT 0,
    `outputTokens` INTEGER NOT NULL DEFAULT 0,
    `estimatedCost` DOUBLE NOT NULL DEFAULT 0,
    `latencyMs` INTEGER NOT NULL DEFAULT 0,
    `success` BOOLEAN NOT NULL DEFAULT true,
    `errorMessage` VARCHAR(500) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `AiUsageLog_userId_idx`(`userId`),
    INDEX `AiUsageLog_feature_idx`(`feature`),
    INDEX `AiUsageLog_createdAt_idx`(`createdAt`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable: WaitlistEntry
CREATE TABLE `WaitlistEntry` (
    `id` VARCHAR(191) NOT NULL,
    `nickname` VARCHAR(100) NOT NULL,
    `email` VARCHAR(255) NOT NULL,
    `devices` JSON NULL,
    `interests` JSON NULL,
    `painpoint` TEXT NULL,
    `willingBeta` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `WaitlistEntry_email_idx`(`email`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `AiUsageLog` ADD CONSTRAINT `AiUsageLog_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
