CREATE TABLE IF NOT EXISTS comments ( id TEXT PRIMARY KEY, document_id TEXT NOT NULL REFERENCES documents(id) ON DELETE CASCADE, version_hash TEXT NOT NULL, parent_id TEXT REFERENCES comments(id) ON DELETE CASCADE, author_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE, content TEXT NOT NULL, anchor_hash TEXT, anchor_line INTEGER, created_at TEXT NOT NULL, resolved_at TEXT, resolved_by TEXT REFERENCES users(id) ON DELETE SET NULL ); CREATE INDEX IF NOT EXISTS idx_comments_document ON comments(document_id); CREATE INDEX IF NOT EXISTS idx_comments_parent ON comments(parent_id); CREATE INDEX IF NOT EXISTS idx_comments_author ON comments(author_id); CREATE TABLE IF NOT EXISTS notifications ( id TEXT PRIMARY KEY, user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE, type TEXT NOT NULL CHECK(type IN ('file_changed', 'comment', 'mention', 'conflict')), resource_type TEXT NOT NULL, resource_id TEXT NOT NULL, message TEXT NOT NULL, read_at TEXT, emailed_at TEXT, created_at TEXT NOT NULL ); CREATE INDEX IF NOT EXISTS idx_notifications_user ON notifications(user_id); CREATE INDEX IF NOT EXISTS idx_notifications_unread ON notifications(user_id, read_at) WHERE read_at IS NULL; CREATE INDEX IF NOT EXISTS idx_notifications_created_at ON notifications(created_at); CREATE TABLE IF NOT EXISTS watchers ( user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE, document_id TEXT REFERENCES documents(id) ON DELETE CASCADE, folder_path TEXT, created_at TEXT NOT NULL, PRIMARY KEY (user_id, document_id, folder_path) ); CREATE INDEX IF NOT EXISTS idx_watchers_document ON watchers(document_id); CREATE INDEX IF NOT EXISTS idx_watchers_folder ON watchers(folder_path);