-- 系统配置表
CREATE TABLE IF NOT EXISTS system_config (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    config_key VARCHAR(100) NOT NULL UNIQUE,
    config_value TEXT,
    value_type VARCHAR(20) NOT NULL DEFAULT 'STRING',
    description VARCHAR(500),
    category VARCHAR(20) NOT NULL DEFAULT 'SYSTEM',
    is_system BOOLEAN NOT NULL DEFAULT FALSE,
    default_value TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_system_config_key ON system_config(config_key);
CREATE INDEX IF NOT EXISTS idx_system_config_category ON system_config(category);

-- 初始化默认配置
INSERT INTO system_config (config_key, config_value, value_type, description, category, is_system, default_value)
SELECT 'system.name', '蚊子系统', 'STRING', '系统名称', 'SYSTEM', TRUE, '蚊子系统'
WHERE NOT EXISTS (SELECT 1 FROM system_config WHERE config_key = 'system.name');

INSERT INTO system_config (config_key, config_value, value_type, description, category, is_system, default_value)
SELECT 'system.version', '1.0.0', 'STRING', '系统版本', 'SYSTEM', TRUE, '1.0.0'
WHERE NOT EXISTS (SELECT 1 FROM system_config WHERE config_key = 'system.version');

INSERT INTO system_config (config_key, config_value, value_type, description, category, is_system, default_value)
SELECT 'system.timezone', 'Asia/Shanghai', 'STRING', '系统时区', 'SYSTEM', TRUE, 'Asia/Shanghai'
WHERE NOT EXISTS (SELECT 1 FROM system_config WHERE config_key = 'system.timezone');

INSERT INTO system_config (config_key, config_value, value_type, description, category, is_system, default_value)
SELECT 'reward.points.enabled', 'true', 'BOOLEAN', '积分奖励是否启用', 'REWARD', TRUE, 'true'
WHERE NOT EXISTS (SELECT 1 FROM system_config WHERE config_key = 'reward.points.enabled');

INSERT INTO system_config (config_key, config_value, value_type, description, category, is_system, default_value)
SELECT 'reward.coupon.enabled', 'true', 'BOOLEAN', '优惠券奖励是否启用', 'REWARD', TRUE, 'true'
WHERE NOT EXISTS (SELECT 1 FROM system_config WHERE config_key = 'reward.coupon.enabled');

INSERT INTO system_config (config_key, config_value, value_type, description, category, is_system, default_value)
SELECT 'risk.alert.enabled', 'true', 'BOOLEAN', '风控告警是否启用', 'SECURITY', TRUE, 'true'
WHERE NOT EXISTS (SELECT 1 FROM system_config WHERE config_key = 'risk.alert.enabled');

INSERT INTO system_config (config_key, config_value, value_type, description, category, is_system, default_value)
SELECT 'risk.auto.block', 'false', 'BOOLEAN', '风控自动拦截', 'SECURITY', TRUE, 'false'
WHERE NOT EXISTS (SELECT 1 FROM system_config WHERE config_key = 'risk.auto.block');
