-- Migration: 007 – Enhance user_messages with status lifecycle and timestamps
-- Database: SQLite
--
-- SQLite does not support ADD COLUMN with constraints referencing other columns
-- or backfill in a single ALTER, so we do it in two steps.

ALTER TABLE user_messages ADD COLUMN status      TEXT    NOT NULL DEFAULT 'sent';
ALTER TABLE user_messages ADD COLUMN received_at DATETIME;
ALTER TABLE user_messages ADD COLUMN deleted_at  DATETIME;

-- Backfill already-read rows.
UPDATE user_messages
   SET status      = 'read',
       received_at = created_at
 WHERE is_read = 1;

CREATE INDEX IF NOT EXISTS idx_user_messages_status ON user_messages(user_id, status);
