CREATE TABLE IF NOT EXISTS "chats" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "title" text, "atlas_user_id" uuid NOT NULL, "created_at" timestamp DEFAULT now() );
SELECT c.id, c.title FROM chats c INNER JOIN ( SELECT chat_id, COUNT(*) AS message_count FROM messages GROUP BY chat_id ) m ON c.id = m.chat_id WHERE m.message_count >= 4;