-- 000042_governance.sql — 治理增强:工具限流与审批、资源/渠道权限申请(全类型审批流)。

-- 工具治理:调用频率上限(0 = 不限)与审批标记(approval_required 工具首次调用需管理员审批)。
ALTER TABLE gateway.tool_definitions
    ADD COLUMN IF NOT EXISTS rate_limit_rpm integer NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS approval_required boolean NOT NULL DEFAULT false;

COMMENT ON COLUMN gateway.tool_definitions.rate_limit_rpm IS
    'Per-tool rate limit in calls per minute, 0 = unlimited. Enforced with an atomic fixed-window upsert so multiple gateway replicas share the same budget.';
COMMENT ON COLUMN gateway.tool_definitions.approval_required IS
    'When true the tool cannot run until an admin approves it; the first invocation creates a tool_approval_requests entry.';

-- 工具限流固定窗口计数(PostgreSQL 原子 upsert,多实例共享)。
CREATE TABLE IF NOT EXISTS gateway.tool_rate_usage (
    tool_id uuid NOT NULL REFERENCES gateway.tool_definitions(id) ON DELETE CASCADE,
    window_start timestamptz NOT NULL,
    call_count bigint NOT NULL DEFAULT 0,
    PRIMARY KEY (tool_id, window_start)
);

-- 工具审批申请:每工具至多一个待审项;审批通过后该工具可被调用。
CREATE TABLE IF NOT EXISTS gateway.tool_approval_requests (
    id uuid PRIMARY KEY,
    tool_id uuid NOT NULL REFERENCES gateway.tool_definitions(id) ON DELETE CASCADE,
    requester_kind varchar(16) NOT NULL DEFAULT 'system',
    requester_id text NOT NULL DEFAULT '',
    reason text NOT NULL DEFAULT '' CHECK (length(reason) <= 4000),
    status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected')),
    decided_by uuid REFERENCES gateway.admin_accounts(id) ON DELETE SET NULL,
    decision_note text NOT NULL DEFAULT '' CHECK (length(decision_note) <= 4000),
    created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
    decided_at timestamptz
);

CREATE UNIQUE INDEX IF NOT EXISTS tool_approval_requests_one_pending_idx
    ON gateway.tool_approval_requests (tool_id) WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS tool_approval_requests_status_time_idx
    ON gateway.tool_approval_requests (status, created_at DESC);

-- 资源/渠道权限申请:门户用户申请 mcp/skill/数字员工/渠道的使用权限,管理员审批后
-- 自动开通(marketplace 安装或渠道部门授权)。
CREATE TABLE IF NOT EXISTS gateway.resource_access_requests (
    id uuid PRIMARY KEY,
    portal_user_id uuid NOT NULL REFERENCES gateway.portal_users(id) ON DELETE CASCADE,
    resource_type varchar(24) NOT NULL CHECK (resource_type IN ('mcp_server', 'skill', 'digital_employee', 'channel')),
    resource_code text NOT NULL CHECK (length(resource_code) BETWEEN 1 AND 128),
    reason text NOT NULL DEFAULT '' CHECK (length(reason) <= 4000),
    status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected', 'cancelled')),
    decision_note text NOT NULL DEFAULT '' CHECK (length(decision_note) <= 4000),
    decided_by uuid REFERENCES gateway.admin_accounts(id) ON DELETE SET NULL,
    decided_at timestamptz,
    created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
    updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
);

CREATE UNIQUE INDEX IF NOT EXISTS resource_access_requests_one_pending_idx
    ON gateway.resource_access_requests (portal_user_id, resource_type, resource_code)
    WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS resource_access_requests_status_time_idx
    ON gateway.resource_access_requests (status, created_at DESC);
