-- MCP 服务与能力表。

CREATE TABLE IF NOT EXISTS mcp_server (
    id BIGSERIAL PRIMARY KEY,
    server_code VARCHAR(100) NOT NULL,
    server_name VARCHAR(200) NOT NULL,
    import_type VARCHAR(50) NOT NULL,
    endpoint_url VARCHAR(500),
    package_name VARCHAR(200),
    manifest_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    auth_type VARCHAR(50),
    secret_ref VARCHAR(200),
    health_status VARCHAR(50) NOT NULL DEFAULT 'UNKNOWN',
    enabled BOOLEAN NOT NULL DEFAULT TRUE,
    version INTEGER NOT NULL DEFAULT 1,
    create_time TIMESTAMP,
    update_time TIMESTAMP,
    remark VARCHAR(500) DEFAULT '',
    create_by VARCHAR(64),
    update_by VARCHAR(64),
    CONSTRAINT uk_mcp_server_code UNIQUE (server_code)
);

CREATE TABLE IF NOT EXISTS mcp_capability (
    id BIGSERIAL PRIMARY KEY,
    server_id BIGINT NOT NULL,
    capability_code VARCHAR(150) NOT NULL,
    capability_name VARCHAR(200) NOT NULL,
    capability_type VARCHAR(50) NOT NULL,
    schema_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    enabled BOOLEAN NOT NULL DEFAULT TRUE,
    version INTEGER NOT NULL DEFAULT 1,
    create_time TIMESTAMP,
    update_time TIMESTAMP,
    remark VARCHAR(500) DEFAULT '',
    create_by VARCHAR(64),
    update_by VARCHAR(64),
    CONSTRAINT uk_mcp_capability_code UNIQUE (server_id, capability_code),
    CONSTRAINT fk_mcp_capability_server_id FOREIGN KEY (server_id) REFERENCES mcp_server (id)
);

CREATE INDEX IF NOT EXISTS idx_mcp_server_import_type ON mcp_server (import_type);
CREATE INDEX IF NOT EXISTS idx_mcp_server_health_status ON mcp_server (health_status);
CREATE INDEX IF NOT EXISTS idx_mcp_capability_server_id ON mcp_capability (server_id);
CREATE INDEX IF NOT EXISTS idx_mcp_capability_type ON mcp_capability (capability_type);

COMMENT ON TABLE mcp_server IS 'MCP服务表';
COMMENT ON TABLE mcp_capability IS 'MCP能力表';
