-- Migration: Create rooms table for chat
-- Description: Chat rooms for real-time messaging

CREATE TABLE IF NOT EXISTS rooms (
    id BIGSERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    room_type VARCHAR(50) NOT NULL DEFAULT 'public', -- 'public', 'private', 'direct'
    creator_id BIGINT NOT NULL,
    is_active BOOLEAN DEFAULT true,
    max_members INTEGER DEFAULT 100,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    
    CONSTRAINT fk_rooms_creator_id FOREIGN KEY (creator_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_rooms_creator_id ON rooms(creator_id);
CREATE INDEX IF NOT EXISTS idx_rooms_room_type ON rooms(room_type);
CREATE INDEX IF NOT EXISTS idx_rooms_is_active ON rooms(is_active) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_rooms_deleted_at ON rooms(deleted_at);
CREATE INDEX IF NOT EXISTS idx_rooms_created_at ON rooms(created_at DESC);

-- Comments
COMMENT ON TABLE rooms IS 'Chat rooms for real-time messaging';
COMMENT ON COLUMN rooms.room_type IS 'Type of room: public, private, or direct';
COMMENT ON COLUMN rooms.max_members IS 'Maximum number of members allowed in the room';

