-- =============================================================================
-- MIGRATION: Recency signal + delivery/read receipts
-- Run against: chat_bot (after 001_init_chat_schema.sql)
-- Convention: additive-only ALTER TABLE ... ADD COLUMN IF NOT EXISTS, matching
--   how the main platform patches existing tables (see its manual/001*.sql).
--   Never edit 001 in place once applied — new columns get their own file.
-- =============================================================================

BEGIN;

-- chat_sessions.lastSeenAt — coarse "was this session active recently" signal
-- for a future merchant-facing "recent chats" list (e.g. a green dot for
-- sessions seen in the last couple minutes). Deliberately separate from
-- lastMessageAt: that's "last message sent", this is "last time the widget
-- was open/connected", which can differ if a shopper reopens the panel
-- without sending anything.
ALTER TABLE "chat"."chat_sessions"
    ADD COLUMN IF NOT EXISTS "lastSeenAt" TIMESTAMPTZ;

COMMENT ON COLUMN "chat"."chat_sessions"."lastSeenAt" IS 'Last time the widget was open/connected for this session — distinct from lastMessageAt';

-- chat_messages.deliveredAt / readAt — persisted delivery/read receipts.
-- Nullable: a message with deliveredAt = NULL simply hasn't been confirmed
-- delivered yet (e.g. socket was disconnected when it was sent).
ALTER TABLE "chat"."chat_messages"
    ADD COLUMN IF NOT EXISTS "deliveredAt" TIMESTAMPTZ,
    ADD COLUMN IF NOT EXISTS "readAt"      TIMESTAMPTZ;

COMMENT ON COLUMN "chat"."chat_messages"."deliveredAt" IS 'Set once the recipient socket has ack''d receipt; NULL = not yet confirmed delivered';
COMMENT ON COLUMN "chat"."chat_messages"."readAt"      IS 'Set when the recipient has opened/viewed the widget panel containing this message';

COMMIT;
