-- Migration 005: Separate sender and recipient keys
-- Adds sender-specific decryption key with 5-minute expiry window

ALTER TABLE customers
  ADD COLUMN IF NOT EXISTS sender_decryption_key    TEXT,
  ADD COLUMN IF NOT EXISTS sender_key_expires_at    TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS sender_opened_at         TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS sender_key_used          BOOLEAN DEFAULT FALSE,
  ADD COLUMN IF NOT EXISTS recipient_notified_sender BOOLEAN DEFAULT FALSE;

-- Index for tmp key cleanup job
CREATE INDEX IF NOT EXISTS idx_customers_sender_key_expires
  ON customers(sender_key_expires_at)
  WHERE sender_key_expires_at IS NOT NULL;
