-- PPE违规报警接收表。
-- 本脚本用于已有数据库增量部署，不会删除现有巡检数据。

CREATE TABLE IF NOT EXISTS `inspection_detection_alert` (
  `id` varchar(32) NOT NULL COMMENT '主键ID',
  `event_id` varchar(128) NOT NULL COMMENT '边缘端事件ID，用于重复过滤',
  `idempotency_key` varchar(191) DEFAULT NULL COMMENT 'HTTP幂等键，仅记录',
  `schema_version` varchar(64) NOT NULL COMMENT '消息结构版本',
  `source_id` varchar(128) DEFAULT NULL COMMENT '视频源标识',
  `grpc_ip` varchar(64) DEFAULT NULL COMMENT '边缘端gRPC服务IP',
  `source_sequence` bigint DEFAULT NULL COMMENT '视频源内消息序号',
  `captured_at_ns` bigint DEFAULT NULL COMMENT '采集时间Unix纳秒',
  `received_at_ns` bigint DEFAULT NULL COMMENT '边缘端接收时间Unix纳秒',
  `trace_id` varchar(128) DEFAULT NULL COMMENT '链路追踪ID',
  `session_id` varchar(128) DEFAULT NULL COMMENT '边缘推理会话ID',
  `input_port` varchar(128) DEFAULT NULL COMMENT '消息输入端口',
  `rule_id` varchar(128) DEFAULT NULL COMMENT '命中的规则ID',
  `model_id` varchar(255) DEFAULT NULL COMMENT '模型版本ID',
  `model_name` varchar(255) DEFAULT NULL COMMENT '模型名称',
  `labels_json` text COMMENT '命中标签JSON',
  `detection_scope` varchar(64) DEFAULT NULL COMMENT '规则统计范围',
  `scope_id` varchar(128) DEFAULT NULL COMMENT '统计范围对象ID',
  `hit_count` int DEFAULT NULL COMMENT '窗口内命中次数',
  `window_ms` decimal(12,3) DEFAULT NULL COMMENT '统计窗口毫秒',
  `first_seen_ns` bigint DEFAULT NULL COMMENT '首次命中时间Unix纳秒',
  `last_seen_ns` bigint DEFAULT NULL COMMENT '最后命中时间Unix纳秒',
  `triggered_at_ns` bigint DEFAULT NULL COMMENT '报警触发时间Unix纳秒',
  `max_confidence` decimal(8,6) DEFAULT NULL COMMENT '最大置信度',
  `image_path` varchar(1000) DEFAULT NULL COMMENT 'MinIO图片URL',
  `raw_json` mediumtext COMMENT '已移除Base64图片数据的原始协议JSON',
  `created_at` datetime(3) NOT NULL COMMENT '平台接收时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_detection_alert_event_id` (`event_id`),
  KEY `idx_detection_alert_source_id` (`source_id`),
  KEY `idx_detection_alert_rule_id` (`rule_id`),
  KEY `idx_detection_alert_triggered_at` (`triggered_at_ns`),
  KEY `idx_detection_alert_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='巡检PPE违规报警主表';

SET @exist_grpc_ip := (
  SELECT COUNT(1)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'inspection_detection_alert'
    AND column_name = 'grpc_ip'
);
SET @add_grpc_ip_sql := IF(
  @exist_grpc_ip = 0,
  'ALTER TABLE `inspection_detection_alert` ADD COLUMN `grpc_ip` varchar(64) DEFAULT NULL COMMENT ''边缘端gRPC服务IP'' AFTER `source_id`',
  'SELECT 1'
);
PREPARE add_grpc_ip_stmt FROM @add_grpc_ip_sql;
EXECUTE add_grpc_ip_stmt;
DEALLOCATE PREPARE add_grpc_ip_stmt;

SET @exist_idempotency_unique := (
  SELECT COUNT(1)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'inspection_detection_alert'
    AND index_name = 'uk_detection_alert_idempotency_key'
);
SET @drop_idempotency_unique_sql := IF(
  @exist_idempotency_unique > 0,
  'ALTER TABLE `inspection_detection_alert` DROP INDEX `uk_detection_alert_idempotency_key`',
  'SELECT 1'
);
PREPARE drop_idempotency_unique_stmt FROM @drop_idempotency_unique_sql;
EXECUTE drop_idempotency_unique_stmt;
DEALLOCATE PREPARE drop_idempotency_unique_stmt;

ALTER TABLE `inspection_detection_alert`
  MODIFY COLUMN `event_id` varchar(128) NOT NULL COMMENT '边缘端事件ID，用于重复过滤',
  MODIFY COLUMN `idempotency_key` varchar(191) DEFAULT NULL COMMENT 'HTTP幂等键，仅记录';
