CREATE TABLE IF NOT EXISTS message_action_confirms (
  app_code VARCHAR(32) NOT NULL COMMENT 'App 租户编码',
  confirm_id VARCHAR(96) NOT NULL COMMENT 'App 操作确认 ID',
  source_name VARCHAR(64) NOT NULL COMMENT '来源事实名，例如 user_outbox',
  source_event_id VARCHAR(128) NOT NULL COMMENT '来源 outbox event_id',
  business_type VARCHAR(64) NOT NULL COMMENT '业务类型，例如 role_invitation',
  business_subtype VARCHAR(64) NOT NULL COMMENT '业务子类型，例如 host/agency/bd',
  business_id VARCHAR(128) NOT NULL COMMENT '业务 owner 的事实 ID',
  sender_user_id BIGINT NOT NULL COMMENT 'IM 发送方用户 ID',
  receiver_user_id BIGINT NOT NULL COMMENT 'IM 接收方用户 ID',
  message_text VARCHAR(512) NOT NULL DEFAULT '' COMMENT 'IM 展示文案快照',
  status VARCHAR(32) NOT NULL COMMENT 'pending/processing/accepted/rejected/expired/canceled/failed',
  action VARCHAR(32) NOT NULL DEFAULT '' COMMENT '终态动作 accept/reject',
  command_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '处理动作 command_id',
  processed_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '处理完成 UTC epoch ms',
  locked_by VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'processing 锁持有者',
  lock_until_ms BIGINT NOT NULL DEFAULT 0 COMMENT 'processing 锁过期 UTC epoch ms',
  error_message VARCHAR(512) NOT NULL DEFAULT '' COMMENT '最后一次处理失败原因',
  metadata_json JSON NULL COMMENT '业务展示扩展快照',
  created_at_ms BIGINT NOT NULL COMMENT '创建 UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新 UTC epoch ms',
  PRIMARY KEY (app_code, confirm_id),
  UNIQUE KEY uk_message_action_source (app_code, source_name, source_event_id),
  KEY idx_message_action_receiver_status (app_code, receiver_user_id, status, created_at_ms, confirm_id),
  KEY idx_message_action_conversation (app_code, receiver_user_id, sender_user_id, created_at_ms, confirm_id),
  KEY idx_message_action_lock (app_code, status, lock_until_ms, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='App IM 操作确认消息状态';

CREATE TABLE IF NOT EXISTS message_action_outbox (
  app_code VARCHAR(32) NOT NULL COMMENT 'App 租户编码',
  event_id VARCHAR(128) NOT NULL COMMENT '确认消息 outbox 事件 ID',
  confirm_id VARCHAR(96) NOT NULL COMMENT '关联 confirm_id',
  event_type VARCHAR(96) NOT NULL COMMENT '事件类型，例如 ConfirmMessageCreated',
  status VARCHAR(32) NOT NULL COMMENT 'pending/running/retryable/delivered/failed',
  worker_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'MQ 投递 worker ID',
  lock_until_ms BIGINT NOT NULL DEFAULT 0 COMMENT 'MQ 投递锁过期 UTC epoch ms',
  retry_count INT NOT NULL DEFAULT 0 COMMENT '投递重试次数',
  next_retry_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '下一次投递 UTC epoch ms',
  last_error VARCHAR(512) NOT NULL DEFAULT '' COMMENT '最后一次投递失败原因',
  payload_json JSON NOT NULL COMMENT '投递给 notice-service 的确认 IM 快照',
  created_at_ms BIGINT NOT NULL COMMENT '创建 UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新 UTC epoch ms',
  PRIMARY KEY (app_code, event_id),
  UNIQUE KEY uk_message_action_outbox_confirm (app_code, confirm_id, event_type),
  KEY idx_message_action_outbox_status_created (app_code, status, next_retry_at_ms, created_at_ms, event_id),
  KEY idx_message_action_outbox_lock (app_code, status, lock_until_ms, created_at_ms, event_id),
  KEY idx_message_action_outbox_retention (app_code, status, updated_at_ms, event_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='确认消息 IM 投递 outbox';
