-- Muse Meta 模块初始 Schema（PostgreSQL 16）
-- 影响范围：MetaSchema、字段、可见性策略、保护节点、功能链路。

CREATE TABLE muse_meta_schema (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    schema_key VARCHAR(100) NOT NULL,
    domain VARCHAR(20) NOT NULL,
    scope VARCHAR(20) NOT NULL,
    target_type VARCHAR(50),
    display_name VARCHAR(200) NOT NULL,
    active_version_id BIGINT,
    effective_scope JSONB,
    projection_version INT NOT NULL DEFAULT 1,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    CONSTRAINT uk_muse_meta_schema_key UNIQUE (tenant_id, domain, scope, target_type, schema_key)
);

CREATE TRIGGER trg_muse_meta_schema_updated_at BEFORE UPDATE ON muse_meta_schema FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_meta_schema_version (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    schema_id BIGINT NOT NULL,
    schema_key VARCHAR(100) NOT NULL,
    version_no INT NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'draft',
    active_flag BOOLEAN NOT NULL DEFAULT FALSE,
    effective_scope JSONB,
    field_contract_snapshot JSONB,
    impact_preview_snapshot JSONB,
    rollback_from_version_id BIGINT,
    rollback_to_version_id BIGINT,
    published_by VARCHAR(64),
    published_at TIMESTAMP,
    activated_by VARCHAR(64),
    activated_at TIMESTAMP,
    change_note VARCHAR(500),
    command_id VARCHAR(120),
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    CONSTRAINT uk_muse_meta_schema_version_key UNIQUE (tenant_id, schema_key, version_no)
);

CREATE INDEX idx_muse_meta_schema_ver_sid ON muse_meta_schema_version(tenant_id, schema_id);
CREATE UNIQUE INDEX uk_muse_meta_schema_ver_command ON muse_meta_schema_version(tenant_id, command_id) WHERE command_id IS NOT NULL;
CREATE TRIGGER trg_muse_meta_schema_version_updated_at BEFORE UPDATE ON muse_meta_schema_version FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_meta_field (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    schema_version_id BIGINT NOT NULL,
    field_key VARCHAR(100) NOT NULL,
    display_name VARCHAR(200) NOT NULL,
    field_type VARCHAR(20) NOT NULL,
    is_required BOOLEAN NOT NULL DEFAULT FALSE,
    default_value JSONB,
    enum_values JSONB,
    validation_rules JSONB,
    sort_order INT NOT NULL DEFAULT 0,
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_muse_meta_field_ver ON muse_meta_field(tenant_id, schema_version_id);
CREATE TRIGGER trg_muse_meta_field_updated_at BEFORE UPDATE ON muse_meta_field FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_meta_visibility_policy (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    schema_version_id BIGINT NOT NULL,
    ui_visible BOOLEAN NOT NULL DEFAULT TRUE,
    ai_context BOOLEAN NOT NULL DEFAULT FALSE,
    user_editable BOOLEAN NOT NULL DEFAULT TRUE,
    user_searchable BOOLEAN NOT NULL DEFAULT FALSE,
    exportable BOOLEAN NOT NULL DEFAULT FALSE,
    policy_snapshot JSONB,
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_muse_meta_vis_policy_ver ON muse_meta_visibility_policy(tenant_id, schema_version_id);
CREATE TRIGGER trg_muse_meta_vis_policy_updated_at BEFORE UPDATE ON muse_meta_visibility_policy FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_meta_protection_node (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    node_key VARCHAR(100) NOT NULL,
    display_name VARCHAR(200) NOT NULL,
    owner_module VARCHAR(50) NOT NULL,
    replaceable BOOLEAN NOT NULL DEFAULT FALSE,
    policy_snapshot JSONB,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    CONSTRAINT uk_muse_meta_protection_node UNIQUE (tenant_id, node_key)
);

CREATE TRIGGER trg_muse_meta_protection_node_updated_at BEFORE UPDATE ON muse_meta_protection_node FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_meta_function_chain (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    chain_key VARCHAR(100) NOT NULL,
    active_version VARCHAR(50),
    chain_snapshot JSONB,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    creator VARCHAR(64) NOT NULL DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN NOT NULL DEFAULT FALSE,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    CONSTRAINT uk_muse_meta_function_chain UNIQUE (tenant_id, chain_key)
);

CREATE TRIGGER trg_muse_meta_function_chain_updated_at BEFORE UPDATE ON muse_meta_function_chain FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
