-- Make sender_id nullable to support system messages
ALTER TABLE chat_messages ALTER COLUMN sender_id DROP NOT NULL;

-- Add system message fields
ALTER TABLE chat_messages ADD COLUMN IF NOT EXISTS is_system   BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE chat_messages ADD COLUMN IF NOT EXISTS system_name TEXT;

-- Expand room type to include notifications
ALTER TABLE chat_rooms DROP CONSTRAINT IF EXISTS chat_rooms_type_check;
ALTER TABLE chat_rooms ADD CONSTRAINT chat_rooms_type_check
  CHECK (type IN ('general', 'direct', 'notifications'));

-- One notifications room per hotel
CREATE UNIQUE INDEX IF NOT EXISTS uniq_chat_rooms_notifications_hotel
  ON chat_rooms(hotel_id) WHERE type = 'notifications';
