ALTER TABLE ias_mail_messages
    ADD COLUMN IF NOT EXISTS seen BOOLEAN NOT NULL DEFAULT FALSE;

CREATE INDEX IF NOT EXISTS idx_ias_mail_messages_seen
    ON ias_mail_messages(seen, received_at DESC, id DESC);

INSERT INTO ias_upgrade_logs (
    version,
    released_at,
    title,
    description,
    changes,
    modules
) VALUES (
    '0.2.8',
    '2026-07-07T00:00:00+08:00',
    '新增邮件已读未读状态',
    '保存 IMAP 已读状态并在邮件列表、详情页和 API 中展示与筛选。',
    '[
        "新增邮件已读/未读状态持久化，保存 IMAP \\Seen 标记到 ias_mail_messages.seen。",
        "邮件列表页面新增状态筛选，支持查看全部、未读或已读邮件。",
        "邮件列表卡片和详情页显示已读/未读状态，未读邮件在列表中使用更醒目的样式。",
        "邮件 API GET /v1/mail/messages 支持 seen=true/false 过滤，并在返回的邮件对象中包含 seen 字段。",
        "数据库新增 ias_mail_messages.seen 字段；已有历史邮件默认标记为未读，后续拉取最近邮件时会刷新为 IMAP 当前状态。"
    ]'::jsonb,
    '{
        "ias-common": "0.1.0",
        "ias-ai": "0.1.2",
        "ias-wechat": "0.1.0",
        "ias-context": "0.1.1",
        "ias-http": "0.1.4",
        "ias-mail": "0.1.13",
        "ias-service": "0.1.7",
        "ias-main": "0.2.8"
    }'::jsonb
) ON CONFLICT (version) DO UPDATE SET
    released_at = EXCLUDED.released_at,
    title = EXCLUDED.title,
    description = EXCLUDED.description,
    changes = EXCLUDED.changes,
    modules = EXCLUDED.modules;
