CREATE TABLE IF NOT EXISTS user_recently_viewed (
  user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  deal_id UUID NOT NULL REFERENCES deals(id) ON DELETE CASCADE,
  viewed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  PRIMARY KEY (user_id, deal_id)
);

CREATE INDEX IF NOT EXISTS user_recently_viewed_user_time_idx
  ON user_recently_viewed (user_id, viewed_at DESC);
