{
  "openapi": "3.0.3",
  "info": {
    "title": "0刀验证虚拟卡 Customer API",
    "version": "1.0.0",
    "description": "公开客户对接接口：获取渠道库存、批量激活卡密、查询本系统激活状态。"
  },
  "servers": [
    { "url": "https://actcard.xyz", "description": "Production" }
  ],
  "tags": [
    { "name": "Inventory", "description": "渠道与库存" },
    { "name": "Activation", "description": "卡密激活与查询" },
    { "name": "Operations", "description": "服务状态" }
  ],
  "paths": {
    "/api/public/channels": {
      "get": {
        "tags": ["Inventory"],
        "summary": "获取渠道与实时库存",
        "operationId": "listPublicChannels",
        "responses": {
          "200": {
            "description": "渠道列表",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ChannelsResponse" } } }
          }
        }
      }
    },
    "/api/activate": {
      "post": {
        "tags": ["Activation"],
        "summary": "批量激活卡密",
        "operationId": "activateCodes",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ActivateRequest" } } }
        },
        "responses": {
          "200": {
            "description": "批次处理完成；逐条检查 items[].ok",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ActivateResponse" } } }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "409": {
            "description": "目标渠道无库存",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          }
        }
      }
    },
    "/api/query": {
      "post": {
        "tags": ["Activation"],
        "summary": "查询本系统卡密激活状态",
        "description": "只读取本系统历史记录，不触发上游请求或激活。单次最多 100 个卡密。",
        "operationId": "queryCodes",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/QueryRequest" } } }
        },
        "responses": {
          "200": {
            "description": "查询完成",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/QueryResponse" } } }
          },
          "400": { "$ref": "#/components/responses/BadRequest" }
        }
      }
    },
    "/api/health": {
      "get": {
        "tags": ["Operations"],
        "summary": "健康检查",
        "operationId": "healthCheck",
        "responses": {
          "200": {
            "description": "服务正常",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["ok", "service"],
                  "properties": {
                    "ok": { "type": "boolean", "example": true },
                    "service": { "type": "string", "example": "timoes-activator" }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "responses": {
      "BadRequest": {
        "description": "请求参数错误",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    },
    "schemas": {
      "CodesInput": {
        "oneOf": [
          { "type": "string", "description": "支持换行、逗号或空格分隔", "example": "YOUR-TIMOES-CDK-001\nYOUR-TIMOES-CDK-002" },
          { "type": "array", "items": { "type": "string" }, "example": ["YOUR-TIMOES-CDK-001", "YOUR-TIMOES-CDK-002"] }
        ]
      },
      "Channel": {
        "type": "object",
        "required": ["id", "name", "card_head", "route_type", "provider", "stock"],
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" },
          "card_head": { "type": "string", "example": "451311" },
          "route_type": { "type": "string", "example": "4513" },
          "provider": { "type": "string", "example": "timoes" },
          "address_template_id": { "type": "string", "nullable": true },
          "stock": { "type": "integer", "nullable": true, "minimum": 0, "example": 40 }
        }
      },
      "ChannelsResponse": {
        "type": "object",
        "required": ["ok", "inventory_available", "channels"],
        "properties": {
          "ok": { "type": "boolean", "example": true },
          "inventory_available": { "type": "boolean", "example": true },
          "channels": { "type": "array", "items": { "$ref": "#/components/schemas/Channel" } }
        }
      },
      "ActivateRequest": {
        "type": "object",
        "required": ["codes", "channel_id"],
        "properties": {
          "codes": { "$ref": "#/components/schemas/CodesInput" },
          "channel_id": { "type": "string", "description": "来自 /api/public/channels" },
          "limit": { "type": "integer", "minimum": 1, "maximum": 20, "default": 1 },
          "delayMs": { "type": "integer", "minimum": 0, "default": 120 }
        }
      },
      "BillingAddress": {
        "type": "object",
        "nullable": true,
        "properties": {
          "name": { "type": "string" },
          "street": { "type": "string" },
          "apt": { "type": "string" },
          "city": { "type": "string" },
          "state": { "type": "string" },
          "zip": { "type": "string" },
          "country": { "type": "string" }
        }
      },
      "CardData": {
        "type": "object",
        "properties": {
          "card_type": { "type": "string", "nullable": true },
          "card_number": { "type": "string", "nullable": true },
          "exp": { "type": "string", "nullable": true },
          "cvc": { "type": "string", "nullable": true },
          "limit": { "type": "number" },
          "remaining_minutes": { "type": "number" },
          "is_new": { "type": "boolean", "nullable": true },
          "billing_address": { "$ref": "#/components/schemas/BillingAddress" },
          "activated_at": { "type": "string", "format": "date-time", "nullable": true },
          "expires_at": { "type": "string", "format": "date-time", "nullable": true },
          "channel_id": { "type": "string", "nullable": true },
          "channel_name": { "type": "string", "nullable": true },
          "status": { "type": "string", "example": "active" }
        }
      },
      "ActivateItem": {
        "type": "object",
        "required": ["ok", "code", "attempts"],
        "properties": {
          "ok": { "type": "boolean" },
          "code": { "type": "string" },
          "attempts": { "type": "integer", "minimum": 0, "maximum": 2 },
          "existing": { "type": "boolean", "description": "已存在成功记录时为 true" },
          "data": { "$ref": "#/components/schemas/CardData" },
          "error": { "type": "string" }
        }
      },
      "ActivateResponse": {
        "type": "object",
        "required": ["ok", "total", "success", "failed", "items"],
        "properties": {
          "ok": { "type": "boolean", "example": true },
          "total": { "type": "integer" },
          "success": { "type": "integer" },
          "failed": { "type": "integer" },
          "items": { "type": "array", "items": { "$ref": "#/components/schemas/ActivateItem" } }
        }
      },
      "QueryRequest": {
        "type": "object",
        "required": ["codes"],
        "properties": { "codes": { "$ref": "#/components/schemas/CodesInput" } }
      },
      "QueryItem": {
        "type": "object",
        "required": ["ok", "code", "status", "activated", "message"],
        "properties": {
          "ok": { "type": "boolean", "example": true },
          "code": { "type": "string" },
          "status": { "type": "string", "enum": ["activated", "failed", "unused"] },
          "activated": { "type": "boolean" },
          "message": { "type": "string" },
          "data": { "$ref": "#/components/schemas/CardData" }
        }
      },
      "QueryResponse": {
        "type": "object",
        "required": ["ok", "mode", "total", "activated", "unused", "invalid", "items"],
        "properties": {
          "ok": { "type": "boolean", "example": true },
          "mode": { "type": "string", "example": "query" },
          "total": { "type": "integer" },
          "activated": { "type": "integer" },
          "unused": { "type": "integer" },
          "invalid": { "type": "integer", "example": 0 },
          "items": { "type": "array", "maxItems": 100, "items": { "$ref": "#/components/schemas/QueryItem" } }
        }
      },
      "Error": {
        "type": "object",
        "required": ["ok", "message"],
        "properties": {
          "ok": { "type": "boolean", "example": false },
          "message": { "type": "string" }
        }
      }
    }
  }
}
