-- M-AI-02-05: 新增不可变 AiJobSnapshot 数据模型
-- ADR-002 §6 删除策略：CASCADE on Job delete

CREATE TABLE `AiJobSnapshot` (
    `id` VARCHAR(191) NOT NULL,
    `jobId` VARCHAR(191) NOT NULL,
    `jobType` VARCHAR(64) NOT NULL,
    `schemaVersion` VARCHAR(100) NOT NULL,
    `redactionVersion` VARCHAR(100) NULL,
    `content` JSON NOT NULL,
    `contentHash` VARCHAR(255) NULL,
    `expiresAt` DATETIME(3) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `AiJobSnapshot_jobId_key` (`jobId`),
    INDEX `AiJobSnapshot_jobType_idx` (`jobType`),
    INDEX `AiJobSnapshot_expiresAt_idx` (`expiresAt`),
    INDEX `AiJobSnapshot_createdAt_idx` (`createdAt`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- FK: Job 删除时级联删 Snapshot（ADR-002 §6）
ALTER TABLE `AiJobSnapshot`
  ADD CONSTRAINT `AiJobSnapshot_jobId_fkey`
    FOREIGN KEY (`jobId`) REFERENCES `AiAnalysisJob`(`id`)
    ON DELETE CASCADE ON UPDATE CASCADE;
