{
  "openapi": "3.1.0",
  "info": {
    "title": "RoboCloud Customer API",
    "version": "1.0.0",
    "description": "读取账户、余额、套餐、服务、用量和订阅，并独立开通按量付费服务。"
  },
  "servers": [
    {
      "url": "https://shop.robocloud.store/api/open/v1"
    }
  ],
  "security": [
    {
      "bearerApiKey": []
    },
    {
      "accountSession": []
    }
  ],
  "paths": {
    "/me": {
      "get": {
        "operationId": "getCurrentAccount",
        "summary": "读取账号和余额",
        "responses": {
          "200": {
            "description": "账号和钱包",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/MeResponse"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/AuthenticationRequired"
          }
        }
      }
    },
    "/plans": {
      "get": {
        "operationId": "listPlans",
        "summary": "读取套餐目录",
        "responses": {
          "200": {
            "description": "套餐列表",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data"],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Plan"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/plans/{sku}": {
      "get": {
        "operationId": "getPlan",
        "summary": "读取单个套餐",
        "parameters": [
          {
            "$ref": "#/components/parameters/Sku"
          }
        ],
        "responses": {
          "200": {
            "description": "套餐",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data"],
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/Plan"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/services": {
      "get": {
        "operationId": "listServices",
        "summary": "读取已有服务",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 100
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "服务列表",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data", "meta"],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Service"
                      }
                    },
                    "meta": {
                      "type": "object",
                      "properties": {
                        "nextCursor": {
                          "type": ["string", "null"]
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/services/{serviceId}/connection": {
      "get": {
        "operationId": "getServiceConnection",
        "summary": "读取 Mihomo 兼容的 Clash 订阅和节点",
        "parameters": [
          {
            "$ref": "#/components/parameters/ServiceId"
          }
        ],
        "responses": {
          "200": {
            "description": "连接信息",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConnectionResponse"
                }
              }
            }
          },
          "202": {
            "description": "主动动态配置正在后台生成；响应结构与 200 相同，subscription 暂为 null。",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ConnectionResponse"
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/services/{serviceId}/usage": {
      "get": {
        "operationId": "getServiceUsage",
        "summary": "读取服务用量和累计扣费",
        "parameters": [
          {
            "$ref": "#/components/parameters/ServiceId"
          }
        ],
        "responses": {
          "200": {
            "description": "用量",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UsageResponse"
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/payg/services": {
      "post": {
        "operationId": "createPaygService",
        "summary": "独立开通按量付费服务",
        "description": "需要 payg:open 权限。每个新的幂等键创建一个独立服务。",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 8,
              "maxLength": 128
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreatePaygServiceRequest"
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "开通任务已创建",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreatePaygServiceResponse"
                }
              }
            }
          },
          "200": {
            "description": "相同幂等请求的原结果",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreatePaygServiceResponse"
                }
              }
            }
          },
          "402": {
            "$ref": "#/components/responses/InsufficientBalance"
          },
          "403": {
            "$ref": "#/components/responses/ScopeRequired"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/operations/{operationId}": {
      "get": {
        "operationId": "getOperation",
        "summary": "查询后台任务",
        "parameters": [
          {
            "name": "operationId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "任务状态",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data"],
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/Operation"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerApiKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "RoboCloud API Key"
      },
      "accountSession": {
        "type": "apiKey",
        "in": "cookie",
        "name": "retail_v2_session"
      }
    },
    "parameters": {
      "Sku": {
        "name": "sku",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      },
      "ServiceId": {
        "name": "serviceId",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      }
    },
    "schemas": {
      "Money": {
        "type": "object",
        "required": ["amountMinor", "currency", "formatted"],
        "properties": {
          "amountMinor": {
            "type": "integer"
          },
          "currency": {
            "type": "string",
            "example": "CNY"
          },
          "formatted": {
            "type": "string",
            "example": "¥2.00"
          }
        }
      },
      "Wallet": {
        "type": "object",
        "required": ["balanceMinor", "currency", "formatted"],
        "properties": {
          "balanceMinor": {
            "type": "integer",
            "minimum": 0
          },
          "currency": {
            "type": "string",
            "example": "CNY"
          },
          "formatted": {
            "type": "string",
            "example": "¥128.50"
          },
          "updatedAt": {
            "type": ["string", "null"],
            "format": "date-time"
          }
        }
      },
      "MeResponse": {
        "type": "object",
        "required": ["data"],
        "properties": {
          "data": {
            "type": "object",
            "required": ["account", "wallet"],
            "properties": {
              "account": {
                "type": "object",
                "required": ["id", "email"],
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "email": {
                    "type": "string",
                    "format": "email"
                  }
                }
              },
              "wallet": {
                "$ref": "#/components/schemas/Wallet"
              }
            }
          }
        }
      },
      "Plan": {
        "type": "object",
        "required": ["sku", "name", "category", "billingMode", "apiPurchasable", "planVersion", "openingPrice", "nodeCount", "nodes"],
        "properties": {
          "sku": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "category": {
            "type": "string"
          },
          "billingMode": {
            "type": "string",
            "enum": ["prepaid", "payg"]
          },
          "summary": {
            "type": "string"
          },
          "apiPurchasable": {
            "type": "boolean"
          },
          "planVersion": {
            "type": "integer"
          },
          "openingPrice": {
            "$ref": "#/components/schemas/Money"
          },
          "nodeCount": {
            "type": "integer",
            "minimum": 0
          },
          "nodes": {
            "type": "array",
            "description": "该 SKU 当前可提供的公开节点目录。已购服务的实际节点以 connection 接口为准。",
            "items": {
              "$ref": "#/components/schemas/Node"
            }
          },
          "payg": {
            "type": "object",
            "properties": {
              "basePriceMinorPerGb": {
                "type": "integer"
              },
              "basePriceFormatted": {
                "type": "string"
              },
              "minimumOpeningBalanceMinor": {
                "type": "integer"
              },
              "minimumOpeningBalanceFormatted": {
                "type": "string"
              }
            }
          }
        }
      },
      "Service": {
        "type": "object",
        "required": ["id", "sku", "name", "billingMode", "status", "nodeCount", "subscriptionReady"],
        "properties": {
          "id": {
            "type": "string"
          },
          "sku": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "billingMode": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "deliveryStatus": {
            "type": "string"
          },
          "availableRouteCount": {
            "type": "integer",
            "minimum": 0
          },
          "pendingRouteCount": {
            "type": "integer",
            "minimum": 0
          },
          "activationDeadlineAt": {
            "type": ["string", "null"],
            "format": "date-time"
          },
          "createdAt": {
            "type": ["string", "null"],
            "format": "date-time"
          },
          "expiresAt": {
            "type": ["string", "null"],
            "format": "date-time"
          },
          "nodeCount": {
            "type": "integer"
          },
          "subscriptionReady": {
            "type": "boolean"
          }
        }
      },
      "Node": {
        "type": "object",
        "required": ["id", "name", "countryCode", "countryName", "protocol", "multiplierMilli", "multiplierLabel", "available", "liveStatus"],
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "countryCode": {
            "type": "string"
          },
          "countryName": {
            "type": "string"
          },
          "protocol": {
            "type": "string"
          },
          "multiplierMilli": {
            "type": "integer",
            "minimum": 100,
            "maximum": 10000
          },
          "multiplierLabel": {
            "type": "string"
          },
          "available": {
            "type": "boolean"
          },
          "liveStatus": {
            "type": "string",
            "enum": ["online", "offline", "unknown"]
          },
          "pendingReason": {
            "type": "string"
          }
        }
      },
      "ConnectionResponse": {
        "type": "object",
        "required": ["data"],
        "properties": {
          "data": {
            "type": "object",
            "required": ["serviceId", "revision", "subscription", "nodes", "stale"],
            "properties": {
              "serviceId": {
                "type": "string"
              },
              "revision": {
                "type": "integer"
              },
              "subscription": {
                "type": ["object", "null"],
                "properties": {
                  "format": {
                    "type": "string",
                    "const": "clash"
                  },
                  "url": {
                    "type": "string",
                    "format": "uri",
                    "description": "Mihomo 可直接加载的 Clash YAML 订阅 URL。"
                  },
                  "generatedAt": {
                    "type": ["string", "null"],
                    "format": "date-time"
                  }
                }
              },
              "nodes": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Node"
                }
              },
              "stale": {
                "type": "boolean",
                "description": "true 表示 Controller 暂时不可用，本次返回最后一份可用配置。"
              }
            }
          }
        }
      },
      "UsageResponse": {
        "type": "object",
        "required": ["data"],
        "properties": {
          "data": {
            "type": "object",
            "required": ["serviceId", "traffic", "billing", "wallet"],
            "properties": {
              "serviceId": {
                "type": "string"
              },
              "traffic": {
                "type": "object",
                "properties": {
                  "uploadBytes": {
                    "type": "integer"
                  },
                  "downloadBytes": {
                    "type": "integer"
                  },
                  "rawTotalBytes": {
                    "type": "integer"
                  },
                  "ratedTotalBytes": {
                    "type": "integer"
                  }
                }
              },
              "billing": {
                "type": "object",
                "properties": {
                  "chargedMinor": {
                    "type": "integer"
                  },
                  "chargedFormatted": {
                    "type": "string"
                  },
                  "currency": {
                    "type": "string"
                  }
                }
              },
              "wallet": {
                "type": "object",
                "properties": {
                  "balanceMinor": {
                    "type": "integer"
                  },
                  "formatted": {
                    "type": "string"
                  }
                }
              },
              "updatedAt": {
                "type": ["string", "null"],
                "format": "date-time"
              }
            }
          }
        }
      },
      "CreatePaygServiceRequest": {
        "type": "object",
        "required": ["sku", "expectedPlanVersion", "expectedBasePriceMinorPerGb", "expectedCurrency", "confirmAutomaticDebit"],
        "properties": {
          "sku": {
            "type": "string"
          },
          "expectedPlanVersion": {
            "type": "integer"
          },
          "expectedBasePriceMinorPerGb": {
            "type": "integer"
          },
          "expectedCurrency": {
            "type": "string"
          },
          "confirmAutomaticDebit": {
            "type": "boolean",
            "const": true
          }
        }
      },
      "Operation": {
        "type": "object",
        "required": ["id", "status", "progress"],
        "properties": {
          "id": {
            "type": "string"
          },
          "type": {
            "type": ["string", "null"]
          },
          "status": {
            "type": "string"
          },
          "progress": {
            "type": "integer",
            "minimum": 0,
            "maximum": 100
          },
          "serviceId": {
            "type": ["string", "null"]
          },
          "createdAt": {
            "type": ["string", "null"],
            "format": "date-time"
          },
          "updatedAt": {
            "type": ["string", "null"],
            "format": "date-time"
          }
        }
      },
      "CreatePaygServiceResponse": {
        "type": "object",
        "required": ["data"],
        "properties": {
          "data": {
            "type": "object",
            "required": ["service", "operation", "wallet"],
            "properties": {
              "service": {
                "$ref": "#/components/schemas/Service"
              },
              "operation": {
                "$ref": "#/components/schemas/Operation"
              },
              "wallet": {
                "$ref": "#/components/schemas/Wallet"
              }
            }
          }
        }
      },
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "object",
            "required": ["code", "message", "requestId"],
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              },
              "requestId": {
                "type": ["string", "null"]
              },
              "details": {
                "type": "object",
                "additionalProperties": true
              }
            }
          }
        }
      }
    },
    "responses": {
      "AuthenticationRequired": {
        "description": "需要登录或有效 API Key",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "InsufficientBalance": {
        "description": "余额不足",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "ScopeRequired": {
        "description": "API Key 权限不足",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "NotFound": {
        "description": "资源不存在",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Conflict": {
        "description": "费率或幂等状态冲突",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    }
  }
}
