-- 创建受控系统配置表；具体业务 Key 由所属业务模块注册，不在公共迁移中预置。
CREATE TABLE tb_system_config (
    id bigserial PRIMARY KEY,
    config_key varchar(150) NOT NULL,
    config_value text NOT NULL,
    value_type varchar(20) NOT NULL,
    module varchar(100) NOT NULL,
    description varchar(500) NOT NULL,
    is_readonly boolean NOT NULL DEFAULT false,
    is_sensitive boolean NOT NULL DEFAULT false,
    creator bigint NOT NULL DEFAULT 0,
    updater bigint NOT NULL DEFAULT 0,
    created_at timestamptz NOT NULL DEFAULT NOW(),
    updated_at timestamptz NOT NULL DEFAULT NOW(),
    CONSTRAINT uq_system_config_key UNIQUE (config_key),
    CONSTRAINT ck_system_config_value_type CHECK (value_type IN ('string', 'int', 'bool', 'json'))
);

CREATE INDEX idx_system_config_module_key ON tb_system_config (module, config_key);

COMMENT ON TABLE tb_system_config IS '受控系统配置表';
COMMENT ON COLUMN tb_system_config.id IS '系统配置主键ID';
COMMENT ON COLUMN tb_system_config.config_key IS '全局唯一的稳定配置Key';
COMMENT ON COLUMN tb_system_config.config_value IS '按值类型保存的配置值';
COMMENT ON COLUMN tb_system_config.value_type IS '值类型 string/int/bool/json';
COMMENT ON COLUMN tb_system_config.module IS '配置所属业务模块';
COMMENT ON COLUMN tb_system_config.description IS '配置用途中文说明';
COMMENT ON COLUMN tb_system_config.is_readonly IS '是否禁止通过在线接口修改';
COMMENT ON COLUMN tb_system_config.is_sensitive IS '是否为响应、日志和审计需要脱敏的配置';
COMMENT ON COLUMN tb_system_config.creator IS '首次创建配置的账号ID';
COMMENT ON COLUMN tb_system_config.updater IS '最近更新配置的账号ID';
COMMENT ON COLUMN tb_system_config.created_at IS '系统配置创建时间';
COMMENT ON COLUMN tb_system_config.updated_at IS '系统配置更新时间';
