-- Phase 1: 补充请求日志表，支持日志查询功能
CREATE TABLE IF NOT EXISTS ai_ops_request_logs (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    timestamp     TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    service       VARCHAR(64) NOT NULL,
    path          VARCHAR(256) NOT NULL,
    method        VARCHAR(8)  NOT NULL,
    status_code   INT         NOT NULL,
    latency_ms    DECIMAL(10,3) NOT NULL,
    user_id       VARCHAR(64),
    supplier_id   VARCHAR(64),
    error_code    VARCHAR(64),
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_request_logs_timestamp ON ai_ops_request_logs (timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_request_logs_service ON ai_ops_request_logs (service);
CREATE INDEX IF NOT EXISTS idx_request_logs_path ON ai_ops_request_logs (path);
CREATE INDEX IF NOT EXISTS idx_request_logs_status_code ON ai_ops_request_logs (status_code);
CREATE INDEX IF NOT EXISTS idx_request_logs_user_id ON ai_ops_request_logs (user_id);
CREATE INDEX IF NOT EXISTS idx_request_logs_supplier_id ON ai_ops_request_logs (supplier_id);
CREATE INDEX IF NOT EXISTS idx_request_logs_time_service ON ai_ops_request_logs (timestamp DESC, service);
