-- 客户事实记忆表：跨实例持久化关键事实（目的地、金额、订单、偏好）
CREATE TABLE IF NOT EXISTS customer_fact (
  id             BIGINT AUTO_INCREMENT PRIMARY KEY,
  company_id     BIGINT       NOT NULL,
  external_user_id VARCHAR(128) NOT NULL,
  instance_id    BIGINT       DEFAULT NULL COMMENT '来源实例ID',
  fact_key       VARCHAR(64)  NOT NULL COMMENT '事实键(destination/order_amount/line_name/satisfaction)',
  fact_value     VARCHAR(1024) NOT NULL COMMENT '事实值',
  fact_type      VARCHAR(32)  DEFAULT 'EXTRACTED' COMMENT 'EXTRACTED/INFERRED/MANUAL',
  confidence     DOUBLE       DEFAULT 0.7,
  create_time    DATETIME     DEFAULT CURRENT_TIMESTAMP,
  update_time    DATETIME     DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_user (company_id, external_user_id),
  KEY idx_fact (fact_key, create_time DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='客户事实记忆表(跨实例持久化)';
