-- DM-Conversation-Liste + Unread-Badge Performance.
-- Vorher: 0 Indizes auf direct_messages → jeder Conversation-List-Load
-- (läuft bei JEDEM User beim App-Start fürs Tab-Badge, plus Refetch nach
-- Send/Focus/Back-Nav) war ein Full-Table-Scan + Sort. Skaliert nicht.
--
-- Deploy: pnpm prisma migrate deploy (auf Hetzner, via deploy-from-artifact.sh)

-- Conversation-Liste: DISTINCT ON (partner) ORDER BY partner, created_at DESC.
-- Ein Index pro Richtung (sender/receiver) deckt das OR-Predicate + Sort ab.
CREATE INDEX IF NOT EXISTS "direct_messages_sender_id_created_at_idx"
    ON "rebreak"."direct_messages" ("sender_id", "created_at" DESC);

CREATE INDEX IF NOT EXISTS "direct_messages_receiver_id_created_at_idx"
    ON "rebreak"."direct_messages" ("receiver_id", "created_at" DESC);

-- Unread-Badge: WHERE receiver_id = $1 AND read_at IS NULL.
CREATE INDEX IF NOT EXISTS "direct_messages_receiver_id_read_at_idx"
    ON "rebreak"."direct_messages" ("receiver_id", "read_at");
