CREATE TABLE IF NOT EXISTS `automation_email_once_deliveries` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    `rule_id` INT UNSIGNED NOT NULL,
    `action_id` INT UNSIGNED NOT NULL,
    `order_id` BIGINT UNSIGNED NOT NULL,
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `auto_email_once_unique` (`rule_id`, `action_id`, `order_id`),
    KEY `auto_email_once_order_idx` (`order_id`),
    CONSTRAINT `auto_email_once_rule_fk` FOREIGN KEY (`rule_id`) REFERENCES `automation_rules` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `auto_email_once_action_fk` FOREIGN KEY (`action_id`) REFERENCES `automation_actions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `auto_email_once_order_fk` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
