-- Rollback: Delete default configuration items
-- Delete all inserted configuration items
DELETE FROM settings WHERE key IN (
    -- Site configuration
    'site.name',
    'site.description',
    'site.keywords',
    'site.url',
    'site.logo',
    'site.copyright',
    'site.icp',
    'site.icp_url',
    'site.color_style',
    
    -- User configuration
    'user.default_avatar',
    'user.default_role',
    'user.register_invite_code',
    'user.register_email_verification',
    'user.open_login',
    'user.open_reset_password',
    
    -- Email configuration
    'email.smtp_host',
    'email.smtp_port',
    'email.smtp_user',
    'email.smtp_password',
    'email.smtp_from',
    'email.smtp_from_name',
    'email.smtp_from_email',
    'email.system_template',
    
    -- Blog configuration
    'blog.default_author',
    'blog.default_category',
    'blog.default_tag',
    'blog.open_comment',
    
    -- Logging configuration
    'logging.level',
    'logging.max_files',
    'logging.max_file_size',
    
    -- Cache configuration
    'cache.ttl',
    'cache.max_size',
    
    -- Feature switches configuration
    'switch.open_register',
    'switch.open_login',
    'switch.open_reset_password',
    'switch.open_comment',
    'switch.open_like',
    'switch.open_share',
    'switch.open_view'
);

-- Drop unique index (if exists)
DROP INDEX IF EXISTS idx_settings_key_unique; 

-- Insert default configuration items
-- Site configuration
INSERT INTO settings (key, value, value_type, description, category, is_system, is_editable) VALUES
('site.name', 'Mapp', 'string', 'Site name', 'site', true, true),
('site.description', 'A modern application platform', 'string', 'Site description', 'site', true, true),
('site.keywords', 'mapp,application,platform', 'string', 'Site keywords', 'site', true, true),
('site.url', 'http://localhost:3000', 'string', 'Site URL', 'site', true, true),
('site.logo', '/static/logo.png', 'string', 'Site logo path', 'site', true, true),
('site.copyright', '© 2024 Mapp. All rights reserved.', 'string', 'Copyright information', 'site', true, true),
('site.icp', '', 'string', 'ICP registration number', 'site', true, true),
('site.icp_url', '', 'string', 'ICP registration URL', 'site', true, true),
('site.color_style', 'default', 'string', 'Site color scheme', 'site', true, true);

-- User configuration
INSERT INTO settings (key, value, value_type, description, category, is_system, is_editable) VALUES
('user.default_avatar', '/static/default-avatar.png', 'string', 'Default user avatar', 'user', true, true),
('user.default_role', 'User', 'string', 'Default user role', 'user', true, true),
('user.register_invite_code', 'true', 'boolean', 'Require invite code for registration', 'user', true, true),
('user.register_email_verification', 'false', 'boolean', 'Require email verification for registration', 'user', true, true),
('user.open_login', 'true', 'boolean', 'Enable login', 'user', true, true),
('user.open_reset_password', 'true', 'boolean', 'Enable password reset', 'user', true, true);

-- Email configuration
INSERT INTO settings (key, value, value_type, description, category, is_system, is_editable) VALUES
('email.smtp_host', '', 'string', 'SMTP server address', 'email', true, true),
('email.smtp_port', '587', 'number', 'SMTP server port', 'email', true, true),
('email.smtp_user', '', 'string', 'SMTP username', 'email', true, true),
('email.smtp_password', '', 'string', 'SMTP password', 'email', true, true),
('email.smtp_from', '', 'string', 'Sender email', 'email', true, true),
('email.smtp_from_name', 'Mapp System', 'string', 'Sender name', 'email', true, true),
('email.smtp_from_email', '', 'string', 'Sender email address', 'email', true, true),
('email.system_template', 'default', 'string', 'System email template', 'email', true, true);

-- Blog configuration
INSERT INTO settings (key, value, value_type, description, category, is_system, is_editable) VALUES
('blog.default_author', 'System', 'string', 'Default blog author', 'blog', true, true),
('blog.default_category', 'Uncategorized', 'string', 'Default blog category', 'blog', true, true),
('blog.default_tag', 'Default', 'string', 'Default blog tag', 'blog', true, true),
('blog.open_comment', 'true', 'boolean', 'Enable comments', 'blog', true, true);

-- Logging configuration
INSERT INTO settings (key, value, value_type, description, category, is_system, is_editable) VALUES
('logging.level', 'info', 'string', 'Log level', 'logging', true, true),
('logging.max_files', '10', 'number', 'Maximum log files', 'logging', true, true),
('logging.max_file_size', '10485760', 'number', 'Maximum log file size (bytes)', 'logging', true, true);

-- Cache configuration
INSERT INTO settings (key, value, value_type, description, category, is_system, is_editable) VALUES
('cache.ttl', '3600', 'number', 'Cache time to live (seconds)', 'cache', true, true),
('cache.max_size', '1000', 'number', 'Maximum cache entries', 'cache', true, true);

-- Feature switches configuration
INSERT INTO settings (key, value, value_type, description, category, is_system, is_editable) VALUES
('switch.open_register', 'true', 'boolean', 'Enable registration', 'switch', true, true),
('switch.open_login', 'true', 'boolean', 'Enable login', 'switch', true, true),
('switch.open_reset_password', 'true', 'boolean', 'Enable password reset', 'switch', true, true),
('switch.open_comment', 'true', 'boolean', 'Enable comments', 'switch', true, true),
('switch.open_like', 'true', 'boolean', 'Enable likes', 'switch', true, true),
('switch.open_share', 'true', 'boolean', 'Enable sharing', 'switch', true, true),
('switch.open_view', 'true', 'boolean', 'Enable view statistics', 'switch', true, true);

-- 创建唯一索引约束（如果不存在）
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_indexes 
        WHERE indexname = 'idx_settings_key_unique' 
        AND tablename = 'settings'
    ) THEN
        CREATE UNIQUE INDEX idx_settings_key_unique ON settings(key);
    END IF;
END $$; 