-- Wablas automation queue and delivery log
CREATE TABLE IF NOT EXISTS `wablas_dispatch_logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `channel` varchar(50) NOT NULL DEFAULT 'wablas',
  `event_type` varchar(50) NOT NULL,
  `event_key` varchar(190) NOT NULL,
  `scheduled_for` datetime DEFAULT NULL,
  `last_attempt_at` datetime DEFAULT NULL,
  `attempt_count` int(11) NOT NULL DEFAULT 0,
  `recipient_count` int(11) NOT NULL DEFAULT 0,
  `status` varchar(50) NOT NULL DEFAULT 'pending',
  `request_payload` longtext DEFAULT NULL,
  `response_payload` longtext DEFAULT NULL,
  `error_message` text DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_wablas_dispatch_event` (`channel`, `event_type`, `event_key`),
  KEY `idx_wablas_dispatch_status` (`status`, `scheduled_for`),
  KEY `idx_wablas_dispatch_attempts` (`attempt_count`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
