[
  {
    "type": "create_table",
    "description": "Create chat_threads table for Two-Way SMS Chat conversation threads",
    "database": "{{store}}",
    "check_query": "SHOW TABLES LIKE 'chat_threads'",
    "sql": "CREATE TABLE `chat_threads` (\n  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n  `typeNum` varchar(20) NOT NULL COMMENT 'Store identifier',\n  `customer_id` int(10) unsigned NOT NULL COMMENT 'Reference to customer',\n  `customer_phone` varchar(15) NOT NULL COMMENT 'Customer phone number',\n  `buy_id` int(10) unsigned DEFAULT NULL COMMENT 'Optional reference to buy transaction',\n  `status` enum('pending','active','closed','archived') NOT NULL DEFAULT 'pending' COMMENT 'Thread status',\n  `staff_can_freetext` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Whether staff can send freeform messages',\n  `last_message_at` timestamp NULL DEFAULT NULL COMMENT 'Timestamp of most recent message',\n  `last_customer_message_at` timestamp NULL DEFAULT NULL COMMENT 'Last message from customer',\n  `last_staff_message_at` timestamp NULL DEFAULT NULL COMMENT 'Last message from staff',\n  `opened_by_employee_id` int(10) unsigned DEFAULT NULL COMMENT 'Employee who opened/activated thread',\n  `closed_by_employee_id` int(10) unsigned DEFAULT NULL COMMENT 'Employee who closed thread',\n  `closed_at` timestamp NULL DEFAULT NULL COMMENT 'When thread was closed',\n  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),\n  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp(),\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `uk_customer_buy` (`typeNum`, `customer_id`, `buy_id`),\n  KEY `idx_status` (`typeNum`, `status`, `last_message_at` DESC),\n  KEY `idx_phone` (`customer_phone`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Two-Way SMS Chat conversation threads'"
  }
]
