-- CreateTable
CREATE TABLE `AiTrace` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `source` VARCHAR(191) NOT NULL,
  `userId` INTEGER NULL,
  `sessionId` INTEGER NULL,
  `model` VARCHAR(191) NULL,
  `prompt` LONGTEXT NULL,
  `rawOutput` LONGTEXT NULL,
  `normalizedOutput` JSON NULL,
  `status` VARCHAR(191) NOT NULL,
  `error` TEXT NULL,
  `durationMs` INTEGER NULL,
  `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updatedAt` DATETIME(3) NOT NULL,

  INDEX `AiTrace_source_createdAt_idx`(`source`, `createdAt`),
  INDEX `AiTrace_userId_createdAt_idx`(`userId`, `createdAt`),
  INDEX `AiTrace_status_createdAt_idx`(`status`, `createdAt`),
  PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `AiTrace` ADD CONSTRAINT `AiTrace_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
