-- Gmail message deduplication table
-- Tracks every Gmail message ID that has been fed into the handler pipeline.
-- Pub/Sub delivers at-least-once and the history API can replay ids, so without
-- this guard a single email can trigger handlers (and notifications) repeatedly.
--
-- Usage: processMessage() attempts INSERT IGNORE before invoking handlers.
-- If affected_rows = 0 the message is already recorded and is skipped.

CREATE TABLE IF NOT EXISTS gmail_processed_messages (
    message_id VARCHAR(128) NOT NULL PRIMARY KEY,
    subject VARCHAR(500) DEFAULT NULL,
    from_addr VARCHAR(255) DEFAULT NULL,
    processed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_processed_at (processed_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
