-- AP mailbox ingestion log
-- Provides idempotency (unique message_id) and audit trail for every processed email.
CREATE TABLE IF NOT EXISTS ap_ingest_log (
  id               BIGINT        AUTO_INCREMENT PRIMARY KEY,
  message_id       VARCHAR(512)  NOT NULL,
  received_at      DATETIME      NULL,
  sender           VARCHAR(255)  NULL,
  subject          TEXT          NULL,
  attachment_count INT           NOT NULL DEFAULT 0,
  invoices_created INT           NOT NULL DEFAULT 0,
  status           ENUM('processed','skipped','error') NOT NULL DEFAULT 'processed',
  error_message    TEXT          NULL,
  created_at       DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_message_id (message_id(512))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
