[
  {
    "type": "create_table",
    "description": "Create chat_messages table for Two-Way SMS Chat message history",
    "database": "{{store}}",
    "check_query": "SHOW TABLES LIKE 'chat_messages'",
    "sql": "CREATE TABLE `chat_messages` (\n  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n  `thread_id` bigint(20) unsigned NOT NULL COMMENT 'Reference to chat_threads',\n  `typeNum` varchar(20) NOT NULL COMMENT 'Store identifier',\n  `direction` enum('inbound','outbound') NOT NULL COMMENT 'Message direction',\n  `sender_type` enum('customer','staff','system') NOT NULL COMMENT 'Who sent the message',\n  `sender_id` int(10) unsigned DEFAULT NULL COMMENT 'Employee ID if staff, NULL otherwise',\n  `template_id` int(10) unsigned DEFAULT NULL COMMENT 'Reference to chat_templates if template used',\n  `content` text NOT NULL COMMENT 'Message content as sent',\n  `content_raw` text DEFAULT NULL COMMENT 'Original content before variable substitution',\n  `character_count` smallint(5) unsigned DEFAULT NULL COMMENT 'Character count of content',\n  `sms_segment_count` tinyint(3) unsigned NOT NULL DEFAULT 1 COMMENT 'Number of SMS segments',\n  `category` enum('transactional','interactive') NOT NULL COMMENT 'Message category for billing',\n  `provider` enum('vonage','twilio') DEFAULT NULL COMMENT 'SMS provider used',\n  `provider_message_id` varchar(255) DEFAULT NULL COMMENT 'External message ID from provider',\n  `delivery_status` enum('pending','queued','sent','delivered','failed','undelivered') NOT NULL DEFAULT 'pending' COMMENT 'Delivery status',\n  `delivery_timestamp` timestamp NULL DEFAULT NULL COMMENT 'When delivery was confirmed',\n  `delivery_error` text DEFAULT NULL COMMENT 'Error message if delivery failed',\n  `read_at` timestamp NULL DEFAULT NULL COMMENT 'When message was read by staff',\n  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),\n  PRIMARY KEY (`id`),\n  KEY `idx_thread` (`thread_id`, `created_at`),\n  KEY `idx_provider_id` (`provider_message_id`),\n  KEY `idx_category` (`typeNum`, `category`, `created_at`),\n  CONSTRAINT `fk_chat_messages_thread` FOREIGN KEY (`thread_id`) REFERENCES `chat_threads` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Two-Way SMS Chat message history'"
  }
]
