CREATE TABLE IF NOT EXISTS qipai_iot_commands (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  device_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NOT NULL,
  room_id BIGINT UNSIGNED NULL,
  order_id BIGINT UNSIGNED NULL,
  command_id VARCHAR(13) NOT NULL,
  command_type VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'PENDING',
  request_payload JSON NOT NULL,
  response_payload JSON NULL,
  trace_id VARCHAR(128) NOT NULL,
  retry_count SMALLINT UNSIGNED NOT NULL DEFAULT 0,
  published_at DATETIME(3) NULL,
  acknowledged_at DATETIME(3) NULL,
  expires_at DATETIME(3) NULL,
  failure_code VARCHAR(64) NOT NULL DEFAULT '',
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
    ON UPDATE CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_iot_command_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_iot_command_device
    FOREIGN KEY (device_id) REFERENCES qipai_devices(id),
  CONSTRAINT fk_qipai_iot_command_store
    FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  CONSTRAINT fk_qipai_iot_command_room
    FOREIGN KEY (room_id) REFERENCES qipai_rooms(id),
  CONSTRAINT fk_qipai_iot_command_order
    FOREIGN KEY (order_id) REFERENCES qipai_orders(id),
  UNIQUE KEY uq_qipai_iot_command_id (tenant_id, command_id),
  KEY idx_qipai_iot_command_status (tenant_id, status, expires_at),
  KEY idx_qipai_iot_command_device (tenant_id, device_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_iot_device_events (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  device_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NOT NULL,
  room_id BIGINT UNSIGNED NULL,
  command_id VARCHAR(13) NULL,
  topic VARCHAR(255) NOT NULL,
  event_type VARCHAR(64) NOT NULL,
  payload_hash CHAR(64) NOT NULL,
  raw_payload JSON NOT NULL,
  normalized_payload JSON NULL,
  event_at DATETIME(3) NULL,
  received_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  receive_count INT UNSIGNED NOT NULL DEFAULT 1,
  processing_status VARCHAR(32) NOT NULL DEFAULT 'RECEIVED',
  CONSTRAINT fk_qipai_iot_event_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_iot_event_device
    FOREIGN KEY (device_id) REFERENCES qipai_devices(id),
  CONSTRAINT fk_qipai_iot_event_store
    FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  CONSTRAINT fk_qipai_iot_event_room
    FOREIGN KEY (room_id) REFERENCES qipai_rooms(id),
  UNIQUE KEY uq_qipai_iot_event_dedup
    (tenant_id, device_id, topic, payload_hash),
  KEY idx_qipai_iot_event_command (tenant_id, command_id),
  KEY idx_qipai_iot_event_status (tenant_id, processing_status, received_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_iot_dead_letters (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NULL,
  device_id BIGINT UNSIGNED NULL,
  topic VARCHAR(255) NOT NULL,
  payload_hash CHAR(64) NOT NULL,
  raw_payload MEDIUMTEXT NOT NULL,
  error_code VARCHAR(64) NOT NULL,
  error_message VARCHAR(500) NOT NULL,
  receive_count INT UNSIGNED NOT NULL DEFAULT 1,
  first_received_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  last_received_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  resolved_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_iot_dead_tenant
    FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_iot_dead_device
    FOREIGN KEY (device_id) REFERENCES qipai_devices(id),
  UNIQUE KEY uq_qipai_iot_dead_dedup (topic, payload_hash),
  KEY idx_qipai_iot_dead_open (resolved_at, last_received_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026062220', 'm06c_iot_messages');
