-- Muse Knowledge 模块初始 Schema（PostgreSQL 16）
-- 影响范围：用户/全局知识库、资料版本、实体关系、草稿、作品绑定。

CREATE TABLE muse_knowledge_base (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name VARCHAR(200) NOT NULL,
    description VARCHAR(500),
    kb_type VARCHAR(20) NOT NULL DEFAULT 'user',
    owner_user_id BIGINT,
    source_market_asset_id BIGINT,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    active_version INT NOT NULL DEFAULT 1,
    visibility_policy JSONB,
    license_snapshot_id BIGINT,
    command_id VARCHAR(120),
    revision INT NOT NULL DEFAULT 1,
    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_kb_owner_status ON muse_knowledge_base(tenant_id, owner_user_id, status);
CREATE INDEX idx_muse_kb_type ON muse_knowledge_base(tenant_id, kb_type);
CREATE UNIQUE INDEX uk_muse_kb_command ON muse_knowledge_base(tenant_id, command_id) WHERE command_id IS NOT NULL;
CREATE TRIGGER trg_muse_knowledge_base_updated_at BEFORE UPDATE ON muse_knowledge_base FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_knowledge_document (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    kb_id BIGINT NOT NULL,
    title VARCHAR(200) NOT NULL,
    file_name VARCHAR(500),
    file_size BIGINT,
    mime_type VARCHAR(100),
    file_hash VARCHAR(100),
    storage_ref VARCHAR(500),
    scan_status VARCHAR(20) NOT NULL DEFAULT 'pending',
    parse_status VARCHAR(20),
    index_status VARCHAR(20),
    retention_policy JSONB,
    author VARCHAR(200),
    source_url VARCHAR(1000),
    tags JSONB,
    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
);

CREATE INDEX idx_muse_kb_doc_kb ON muse_knowledge_document(tenant_id, kb_id, scan_status);
CREATE INDEX idx_muse_kb_doc_hash ON muse_knowledge_document(tenant_id, file_hash);
CREATE UNIQUE INDEX uk_muse_kb_doc_command ON muse_knowledge_document(tenant_id, command_id) WHERE command_id IS NOT NULL;
CREATE TRIGGER trg_muse_knowledge_document_updated_at BEFORE UPDATE ON muse_knowledge_document FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_knowledge_document_version (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    document_id BIGINT NOT NULL,
    version INT NOT NULL,
    file_hash VARCHAR(100) NOT NULL,
    storage_ref VARCHAR(500),
    content_hash VARCHAR(100),
    source_snapshot_id BIGINT,
    scan_status VARCHAR(20) NOT NULL DEFAULT 'pending',
    parse_status VARCHAR(20) NOT NULL DEFAULT 'pending',
    processing_status VARCHAR(20) NOT NULL DEFAULT 'pending',
    previous_version_id BIGINT,
    change_summary JSONB,
    change_reason 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_kb_doc_ver UNIQUE (tenant_id, document_id, version)
);

CREATE INDEX idx_muse_kb_doc_ver_doc ON muse_knowledge_document_version(tenant_id, document_id);
CREATE TRIGGER trg_muse_kb_doc_ver_updated_at BEFORE UPDATE ON muse_knowledge_document_version FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_knowledge_entity (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    work_id BIGINT NOT NULL,
    kb_id BIGINT,
    entity_type VARCHAR(50) NOT NULL,
    normalized_name VARCHAR(200) NOT NULL,
    scope VARCHAR(50) NOT NULL DEFAULT 'global',
    description TEXT,
    attributes JSONB,
    source_snapshot_id BIGINT,
    authorization_snapshot_id BIGINT,
    source_status VARCHAR(30) NOT NULL DEFAULT 'active',
    source_action_policy VARCHAR(30) NOT NULL DEFAULT 'allowed',
    source_event_version INT,
    lineage_payload JSONB,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    confidence DOUBLE PRECISION DEFAULT 1.0,
    source_type VARCHAR(50),
    source_id BIGINT,
    command_id VARCHAR(120),
    revision INT NOT NULL DEFAULT 1,
    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_knowledge_entity UNIQUE (tenant_id, work_id, entity_type, normalized_name, scope)
);

CREATE INDEX idx_muse_knowledge_entity_work_type ON muse_knowledge_entity(tenant_id, work_id, entity_type);
CREATE INDEX idx_muse_knowledge_entity_kb ON muse_knowledge_entity(tenant_id, kb_id);
CREATE UNIQUE INDEX uk_muse_knowledge_entity_command ON muse_knowledge_entity(tenant_id, command_id) WHERE command_id IS NOT NULL;
CREATE TRIGGER trg_muse_knowledge_entity_updated_at BEFORE UPDATE ON muse_knowledge_entity FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_knowledge_relation (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    work_id BIGINT NOT NULL,
    source_entity_id BIGINT NOT NULL,
    target_entity_id BIGINT NOT NULL,
    relation_type VARCHAR(50) NOT NULL,
    description VARCHAR(500),
    attributes JSONB,
    source_status VARCHAR(30) NOT NULL DEFAULT 'active',
    source_action_policy VARCHAR(30) NOT NULL DEFAULT 'allowed',
    command_id VARCHAR(120),
    revision INT NOT NULL DEFAULT 1,
    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_knowledge_rel_work ON muse_knowledge_relation(tenant_id, work_id);
CREATE INDEX idx_muse_knowledge_rel_src ON muse_knowledge_relation(tenant_id, source_entity_id);
CREATE INDEX idx_muse_knowledge_rel_tgt ON muse_knowledge_relation(tenant_id, target_entity_id);
CREATE UNIQUE INDEX uk_muse_knowledge_rel_command ON muse_knowledge_relation(tenant_id, command_id) WHERE command_id IS NOT NULL;
CREATE TRIGGER trg_muse_knowledge_relation_updated_at BEFORE UPDATE ON muse_knowledge_relation FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_knowledge_draft (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    work_id BIGINT NOT NULL,
    entity_id BIGINT,
    draft_type VARCHAR(30) NOT NULL DEFAULT 'entity',
    target_object_id BIGINT,
    proposed_changes JSONB,
    draft_payload JSONB,
    current_canonical_snapshot JSONB,
    source_snapshot_id BIGINT,
    authorization_snapshot_id BIGINT,
    risk_marker_snapshot JSONB,
    source_status VARCHAR(30),
    source_action_policy VARCHAR(30),
    status VARCHAR(20) NOT NULL DEFAULT 'pending',
    confidence DOUBLE PRECISION,
    source_type VARCHAR(50),
    source_id BIGINT,
    command_id VARCHAR(120),
    revision INT NOT NULL DEFAULT 1,
    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_knowledge_draft_work_status ON muse_knowledge_draft(tenant_id, work_id, status, create_time);
CREATE INDEX idx_muse_knowledge_draft_entity ON muse_knowledge_draft(tenant_id, entity_id);
CREATE UNIQUE INDEX uk_muse_knowledge_draft_command ON muse_knowledge_draft(tenant_id, command_id) WHERE command_id IS NOT NULL;
CREATE TRIGGER trg_muse_knowledge_draft_updated_at BEFORE UPDATE ON muse_knowledge_draft FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TABLE muse_knowledge_binding (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    work_id BIGINT NOT NULL,
    kb_id BIGINT NOT NULL,
    binding_type VARCHAR(50) NOT NULL DEFAULT 'read',
    binding_scope VARCHAR(50) NOT NULL DEFAULT 'read',
    binding_status VARCHAR(30) NOT NULL DEFAULT 'active',
    source_snapshot_id BIGINT,
    authorization_snapshot_id BIGINT,
    source_version INT,
    target_version INT,
    command_id VARCHAR(120),
    revision INT NOT NULL DEFAULT 1,
    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_knowledge_binding_work_kb UNIQUE (tenant_id, work_id, kb_id)
);

CREATE INDEX idx_muse_knowledge_binding_work ON muse_knowledge_binding(tenant_id, work_id);
CREATE INDEX idx_muse_knowledge_binding_kb ON muse_knowledge_binding(tenant_id, kb_id);
CREATE UNIQUE INDEX uk_muse_knowledge_binding_command ON muse_knowledge_binding(tenant_id, command_id) WHERE command_id IS NOT NULL;
CREATE TRIGGER trg_muse_knowledge_binding_updated_at BEFORE UPDATE ON muse_knowledge_binding FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
