\set ON_ERROR_STOP on

BEGIN;

INSERT INTO sys_setting (
    key,
    name,
    category,
    description,
    value_type,
    value_json,
    is_public,
    sort_order,
    created_at,
    updated_at
)
VALUES
    ('page_size', '分页大小', 'ui', '平台各类列表默认每页条数。', 'integer', :'page_size_json', TRUE, 5, NOW(), NOW()),
    ('chat_pull_page_size', '对话懒加载条数', 'chat', 'Bot 对话区向上懒加载时每次读取的消息条数。', 'integer', :'chat_pull_page_size_json', TRUE, 8, NOW(), NOW()),
    ('auth_token_ttl_hours', '认证 Token 过期小时数', 'auth', 'Panel 与 Bot 登录 Token 的统一有效时长，单位小时。', 'integer', :'auth_token_ttl_hours_json', FALSE, 10, NOW(), NOW()),
    ('auth_token_max_active', '认证 Token 最大并发数', 'auth', '同一主体允许同时活跃的 Token 数量，超过时自动撤销最旧 Token。', 'integer', :'auth_token_max_active_json', FALSE, 11, NOW(), NOW()),
    ('upload_max_mb', '上传大小限制', 'upload', '单文件上传大小限制，单位 MB。', 'integer', :'upload_max_mb_json', FALSE, 20, NOW(), NOW()),
    ('allowed_attachment_extensions', '允许附件后缀', 'upload', '允许上传的附件后缀列表，留空表示不限制。', 'json', :'allowed_attachment_extensions_json', FALSE, 20, NOW(), NOW()),
    ('workspace_download_extensions', '工作区下载后缀', 'workspace', '命中后缀的工作区文件默认走下载模式。', 'json', :'workspace_download_extensions_json', FALSE, 30, NOW(), NOW()),
    ('speech_enabled', '语音识别开关', 'speech', '控制 Bot 语音转写功能是否启用。', 'boolean', :'speech_enabled_json', TRUE, 32, NOW(), NOW()),
    ('activity_event_retention_days', '活动事件保留天数', 'maintenance', 'bot_activity_event 运维事件的保留天数，超期记录会自动清理。', 'integer', :'activity_event_retention_days_json', FALSE, 34, NOW(), NOW())
ON CONFLICT (key) DO UPDATE
SET
    name = EXCLUDED.name,
    category = EXCLUDED.category,
    description = EXCLUDED.description,
    value_type = EXCLUDED.value_type,
    is_public = EXCLUDED.is_public,
    sort_order = EXCLUDED.sort_order,
    updated_at = NOW();

DELETE FROM sys_setting WHERE key = 'command_auto_unlock_seconds';

INSERT INTO skill_market_item (
    skill_key,
    display_name,
    description,
    zip_filename,
    zip_size_bytes,
    entry_names_json,
    created_at,
    updated_at
)
VALUES
    ('nano-banana-pro', 'Nano Banana Pro', 'Generate and edit images with Nano Banana Pro.', 'nano-banana-pro.zip', 5317, '["nano-banana-pro"]', NOW(), NOW()),
    ('powerpoint-pptx', 'Powerpoint PPTX', 'Create and edit PowerPoint PPTX files.', 'powerpoint-pptx.zip', 3596, '["powerpoint-pptx"]', NOW(), NOW()),
    ('self-improving-agent', 'Self Improving Agent', 'Capture learnings, errors, and corrections for continuous improvement.', 'self-improving-agent.zip', 26866, '["self-improving-agent"]', NOW(), NOW()),
    ('stock-analysis', 'Stock Analysis', 'Analyze stocks and crypto with research and portfolio workflows.', 'stock-analysis.zip', 79536, '["stock-analysis"]', NOW(), NOW()),
    ('tavily-search', 'Tavily Search', 'AI-optimized web search skill powered by Tavily.', 'tavily-search.zip', 3459, '["tavily-search"]', NOW(), NOW()),
    ('writing', 'Writing', 'Adapt writing voice and improve clarity, structure, and style.', 'writing.zip', 8618, '["writing"]', NOW(), NOW())
ON CONFLICT (skill_key) DO UPDATE
SET
    display_name = EXCLUDED.display_name,
    description = EXCLUDED.description,
    zip_filename = EXCLUDED.zip_filename,
    zip_size_bytes = EXCLUDED.zip_size_bytes,
    entry_names_json = EXCLUDED.entry_names_json,
    updated_at = NOW();

COMMIT;
