CREATE TABLE IF NOT EXISTS outbound_emails (
  id INT AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  participant_id INT NULL,
  template VARCHAR(60) NOT NULL,
  subject VARCHAR(255) NOT NULL,
  status VARCHAR(30) NOT NULL,
  sent_at DATETIME NULL,
  error VARCHAR(500) NULL,
  created_by_user_id INT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_outbound_emails_tenant_created (tenant_id, created_at),
  KEY idx_outbound_emails_participant (participant_id),
  CONSTRAINT fk_outbound_emails_tenant
    FOREIGN KEY (tenant_id) REFERENCES tenants(id)
    ON DELETE CASCADE,
  CONSTRAINT fk_outbound_emails_participant
    FOREIGN KEY (participant_id) REFERENCES participants(id)
    ON DELETE SET NULL,
  CONSTRAINT fk_outbound_emails_created_by_user
    FOREIGN KEY (created_by_user_id) REFERENCES users(id)
    ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
