-- M-AI-02-06: 新增 AiJobArtifact 产物关联模型（多态引用）
-- ADR-002 §6 删除策略：CASCADE on Job delete

CREATE TABLE `AiJobArtifact` (
    `id` VARCHAR(191) NOT NULL,
    `jobId` VARCHAR(191) NOT NULL,
    `artifactType` VARCHAR(32) NOT NULL,
    `artifactId` VARCHAR(255) NOT NULL,
    `ordinal` INT NOT NULL DEFAULT 0,
    `metadata` JSON NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `AiJobArtifact_jobId_artifactType_artifactId_key` (`jobId`, `artifactType`, `artifactId`),
    INDEX `AiJobArtifact_jobId_ordinal_idx` (`jobId`, `ordinal`),
    INDEX `AiJobArtifact_artifactType_artifactId_idx` (`artifactType`, `artifactId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

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