-- Online presence
CREATE TABLE IF NOT EXISTS chat_presence (
  user_id   UUID        NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  hotel_id  UUID        NOT NULL REFERENCES hotels(id) ON DELETE CASCADE,
  last_seen TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  PRIMARY KEY (user_id, hotel_id)
);
CREATE INDEX IF NOT EXISTS idx_chat_presence_hotel ON chat_presence(hotel_id, last_seen);

-- Typing indicator (expires after 5s — queried with WHERE typing_at > NOW() - interval '5s')
CREATE TABLE IF NOT EXISTS chat_typing (
  user_id   UUID        NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  room_id   UUID        NOT NULL REFERENCES chat_rooms(id) ON DELETE CASCADE,
  name      TEXT        NOT NULL,
  typing_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  PRIMARY KEY (user_id, room_id)
);
