-- Migration 008: Add recipient_uni_key and sender_downloaded_at
--
-- recipient_uni_key: short permanent identifier for recipient
--   Used only in downloaded filename — never for decryption
--   Visible on postcard viewer page for support reference
--
-- sender_downloaded_at: tracks when sender downloaded their copy
--   Used to show appropriate message when recipient has already opened

ALTER TABLE customers
  ADD COLUMN IF NOT EXISTS recipient_uni_key    TEXT,
  ADD COLUMN IF NOT EXISTS sender_downloaded_at TIMESTAMPTZ;

-- Generate uni keys for existing orders
UPDATE customers
  SET recipient_uni_key = LOWER(ENCODE(GEN_RANDOM_BYTES(4), 'hex'))
  WHERE recipient_uni_key IS NULL;
