-- Add imap_uid column to mails table for proper deduplication
ALTER TABLE mails ADD COLUMN IF NOT EXISTS imap_uid VARCHAR(20);
CREATE INDEX IF NOT EXISTS ix_mails_imap_uid ON mails(account_id, imap_uid);
-- Unique constraint to prevent duplicates per folder
CREATE UNIQUE INDEX IF NOT EXISTS uq_mails_account_folder_uid ON mails(account_id, folder_id, imap_uid) WHERE imap_uid IS NOT NULL;
