-- Migration 141: Store email threading headers for helpdesk case routing

ALTER TABLE email_messages
ADD COLUMN IF NOT EXISTS in_reply_to VARCHAR(500),
ADD COLUMN IF NOT EXISTS email_references TEXT;

CREATE INDEX IF NOT EXISTS idx_email_messages_in_reply_to
ON email_messages(in_reply_to)
WHERE in_reply_to IS NOT NULL;

COMMENT ON COLUMN email_messages.in_reply_to IS 'Raw In-Reply-To header used for SAG-<id> threading lookup';
COMMENT ON COLUMN email_messages.email_references IS 'Raw References header used for SAG-<id> threading lookup';
