-- IoT 规则引擎三张核心表
-- [B2] RuleChain/Node/Link 数据模型

CREATE TABLE IF NOT EXISTS iot_rule_chain (
    id              BIGINT PRIMARY KEY AUTO_INCREMENT,
    name            VARCHAR(128)  NOT NULL          COMMENT '规则链名称',
    description     TEXT                            COMMENT '规则链描述',
    type            VARCHAR(32)   NOT NULL           COMMENT 'SCENE / DATA / CUSTOM',
    status          TINYINT       NOT NULL DEFAULT 1 COMMENT '0=禁用 1=启用 2=WARNING（物模型变更导致）',
    priority        INT           NOT NULL DEFAULT 100 COMMENT 'ASC 排序，评审 A5',
    version         BIGINT        NOT NULL DEFAULT 0  COMMENT '变更时+1，评审 B9 多实例校验用',
    debug_mode      TINYINT       NOT NULL DEFAULT 0  COMMENT '调试模式',

    -- 三层绑定（评审 §十一-B）
    subsystem_id    BIGINT                          COMMENT 'NULL = 全局规则',
    product_id      BIGINT                          COMMENT 'NULL = 不限产品',
    device_id       BIGINT                          COMMENT 'NULL/0 = 范围内所有设备',

    tenant_id       BIGINT        NOT NULL           COMMENT '租户编号',
    creator         VARCHAR(64)                     COMMENT '创建者',
    create_time     DATETIME      DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater         VARCHAR(64)                     COMMENT '更新者',
    update_time     DATETIME      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted         BIT           NOT NULL DEFAULT b'0' COMMENT '是否删除',

    INDEX idx_binding   (tenant_id, subsystem_id, product_id, device_id),
    INDEX idx_status    (tenant_id, status, priority),
    INDEX idx_update_time (update_time)
) COMMENT = '规则链';

CREATE TABLE IF NOT EXISTS iot_rule_node (
    id              BIGINT PRIMARY KEY AUTO_INCREMENT,
    rule_chain_id   BIGINT        NOT NULL           COMMENT '所属规则链编号',
    name            VARCHAR(128)                    COMMENT '节点名称',
    category        VARCHAR(32)   NOT NULL           COMMENT 'trigger / condition / action',
    type            VARCHAR(64)   NOT NULL           COMMENT 'Provider 标识（device_property / alarm_trigger 等）',
    configuration   JSON          NOT NULL           COMMENT '节点配置',
    position_x      INT                             COMMENT '画布 X 坐标',
    position_y      INT                             COMMENT '画布 Y 坐标',
    tenant_id       BIGINT        NOT NULL           COMMENT '租户编号',
    creator         VARCHAR(64)                     COMMENT '创建者',
    create_time     DATETIME      DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater         VARCHAR(64)                     COMMENT '更新者',
    update_time     DATETIME      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted         BIT           NOT NULL DEFAULT b'0' COMMENT '是否删除',

    INDEX idx_chain (rule_chain_id, deleted)
) COMMENT = '规则节点';

CREATE TABLE IF NOT EXISTS iot_rule_link (
    id              BIGINT PRIMARY KEY AUTO_INCREMENT,
    rule_chain_id   BIGINT        NOT NULL           COMMENT '所属规则链编号',
    source_node_id  BIGINT        NOT NULL           COMMENT '源节点编号',
    target_node_id  BIGINT        NOT NULL           COMMENT '目标节点编号',
    relation_type   VARCHAR(32)   NOT NULL           COMMENT '【封闭枚举 评审 B4】Success/Failure/True/False/Timeout/Skip',
    condition       JSON                            COMMENT '连线条件（可选）',
    sort_order      INT                   DEFAULT 0  COMMENT '排序',
    tenant_id       BIGINT        NOT NULL           COMMENT '租户编号',
    creator         VARCHAR(64)                     COMMENT '创建者',
    create_time     DATETIME      DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updater         VARCHAR(64)                     COMMENT '更新者',
    update_time     DATETIME      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted         BIT           NOT NULL DEFAULT b'0' COMMENT '是否删除',

    INDEX idx_source (rule_chain_id, source_node_id, deleted)
) COMMENT = '规则连线';
