{
  "openapi": "3.1.0",
  "info": {
    "title": "KARDS Decker API",
    "version": "1.0.0",
    "description": "KARDS Decker 公共 API v1。普通第三方工具使用公开目录接口或用户自行创建的 kd_ API Key。"
  },
  "servers": [
    {
      "url": "https://1939.giaory.xyz",
      "description": "生产环境"
    }
  ],
  "tags": [
    { "name": "Discovery", "description": "API 入口" },
    { "name": "Catalog", "description": "卡牌和卡背目录" },
    { "name": "Public decks", "description": "无需鉴权的公开卡组" },
    { "name": "User decks", "description": "使用 kd_ API Key 管理自己的卡组" }
  ],
  "paths": {
    "/api/v1": {
      "get": {
        "tags": ["Discovery"],
        "summary": "读取 API 发现信息",
        "operationId": "getApiDiscovery",
        "security": [],
        "responses": {
          "200": {
            "description": "API 版本和公开入口",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/DiscoveryEnvelope" }
              }
            }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/cards": {
      "get": {
        "tags": ["Catalog"],
        "summary": "搜索卡牌目录",
        "operationId": "listCards",
        "security": [],
        "parameters": [
          { "$ref": "#/components/parameters/Page" },
          { "$ref": "#/components/parameters/Limit" },
          { "name": "q", "in": "query", "schema": { "type": "string" }, "description": "搜索 card_id、import_id、名称和描述" },
          { "name": "faction", "in": "query", "schema": { "$ref": "#/components/schemas/Faction" } },
          { "name": "type", "in": "query", "schema": { "type": "string" } },
          { "name": "rarity", "in": "query", "schema": { "type": "string" } },
          { "name": "set", "in": "query", "schema": { "type": "string" } },
          { "name": "pool", "in": "query", "schema": { "type": "string", "enum": ["active", "reserve", "derived"] } }
        ],
        "responses": {
          "200": {
            "description": "分页卡牌列表",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CardListEnvelope" } } }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/cards/{card_id}": {
      "get": {
        "tags": ["Catalog"],
        "summary": "按 card_id 读取卡牌",
        "operationId": "getCard",
        "security": [],
        "parameters": [
          { "$ref": "#/components/parameters/CardId" }
        ],
        "responses": {
          "200": {
            "description": "卡牌详情",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CardEnvelope" } } }
          },
          "404": { "$ref": "#/components/responses/Error" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/card-backs": {
      "get": {
        "tags": ["Catalog"],
        "summary": "搜索卡背目录",
        "operationId": "listCardBacks",
        "security": [],
        "parameters": [
          { "$ref": "#/components/parameters/Page" },
          { "$ref": "#/components/parameters/Limit" },
          { "name": "q", "in": "query", "schema": { "type": "string" }, "description": "搜索卡背 ID 或名称" }
        ],
        "responses": {
          "200": {
            "description": "分页卡背列表",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CardBackListEnvelope" } } }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/decks": {
      "get": {
        "tags": ["Public decks"],
        "summary": "搜索公开卡组",
        "operationId": "listPublicDecks",
        "security": [],
        "parameters": [
          { "$ref": "#/components/parameters/Page" },
          { "$ref": "#/components/parameters/Limit" },
          { "name": "q", "in": "query", "schema": { "type": "string" } },
          { "name": "main_faction", "in": "query", "schema": { "$ref": "#/components/schemas/Faction" } },
          { "name": "ally_faction", "in": "query", "schema": { "$ref": "#/components/schemas/Faction" } },
          { "name": "tag", "in": "query", "schema": { "type": "string" } },
          { "name": "sort", "in": "query", "schema": { "type": "string", "default": "date", "enum": ["date", "likes", "views"] } }
        ],
        "responses": {
          "200": {
            "description": "分页公开卡组列表",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/DeckListEnvelope" } } }
          },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/decks/{share_slug}": {
      "get": {
        "tags": ["Public decks"],
        "summary": "按分享标识读取公开卡组",
        "operationId": "getPublicDeck",
        "security": [],
        "parameters": [
          { "name": "share_slug", "in": "path", "required": true, "schema": { "type": "string", "pattern": "^[a-zA-Z0-9_-]{1,64}$" } }
        ],
        "responses": {
          "200": {
            "description": "公开卡组",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/DeckEnvelope" } } }
          },
          "404": { "$ref": "#/components/responses/Error" },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/users/{user_id}/decks": {
      "get": {
        "tags": ["User decks"],
        "summary": "读取自己的卡组",
        "operationId": "listUserDecks",
        "security": [{ "ApiKeyBearer": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/UserId" },
          { "$ref": "#/components/parameters/Page" },
          { "$ref": "#/components/parameters/Limit" }
        ],
        "responses": {
          "200": { "description": "分页个人卡组列表", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/DeckListEnvelope" } } } },
          "default": { "$ref": "#/components/responses/Error" }
        }
      },
      "post": {
        "tags": ["User decks"],
        "summary": "创建卡组",
        "operationId": "createUserDeck",
        "security": [{ "ApiKeyBearer": [] }],
        "parameters": [{ "$ref": "#/components/parameters/UserId" }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreateDeckInput" } } }
        },
        "responses": {
          "201": { "description": "已创建的卡组", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/DeckEnvelope" } } } },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/users/{user_id}/decks/{deck_id}": {
      "get": {
        "tags": ["User decks"],
        "summary": "读取自己的单个卡组",
        "operationId": "getUserDeck",
        "security": [{ "ApiKeyBearer": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/UserId" },
          { "$ref": "#/components/parameters/DeckId" }
        ],
        "responses": {
          "200": { "description": "个人卡组", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/DeckEnvelope" } } } },
          "default": { "$ref": "#/components/responses/Error" }
        }
      },
      "patch": {
        "tags": ["User decks"],
        "summary": "更新卡组",
        "description": "只更新顶层已提供字段；如果提供 guide，则该对象整体替换现有攻略元数据。",
        "operationId": "updateUserDeck",
        "security": [{ "ApiKeyBearer": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/UserId" },
          { "$ref": "#/components/parameters/DeckId" }
        ],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/UpdateDeckInput" } } }
        },
        "responses": {
          "200": { "description": "更新后的卡组", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/DeckEnvelope" } } } },
          "default": { "$ref": "#/components/responses/Error" }
        }
      },
      "delete": {
        "tags": ["User decks"],
        "summary": "删除卡组",
        "operationId": "deleteUserDeck",
        "security": [{ "ApiKeyBearer": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/UserId" },
          { "$ref": "#/components/parameters/DeckId" }
        ],
        "requestBody": {
          "required": true,
          "description": "发送空 JSON 对象。",
          "content": {
            "application/json": {
              "schema": { "type": "object", "maxProperties": 0 }
            }
          }
        },
        "responses": {
          "200": { "description": "删除结果", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/DeleteEnvelope" } } } },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/users/{user_id}/decks/validate": {
      "post": {
        "tags": ["User decks"],
        "summary": "校验卡组提交但不保存",
        "operationId": "validateUserDeck",
        "security": [{ "ApiKeyBearer": [] }],
        "parameters": [{ "$ref": "#/components/parameters/UserId" }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreateDeckInput" } } }
        },
        "responses": {
          "200": { "description": "规范化后的卡组与攻略", "content": { "application/json": { "schema": { "type": "object" } } } },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/api/v1/users/{user_id}/decks/replace": {
      "post": {
        "tags": ["User decks"],
        "summary": "替换卡牌并重新生成卡组代码",
        "operationId": "replaceUserDeckCards",
        "security": [{ "ApiKeyBearer": [] }],
        "parameters": [{ "$ref": "#/components/parameters/UserId" }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ReplaceDeckInput" } } }
        },
        "responses": {
          "200": { "description": "重新校验后的卡组代码和替换记录", "content": { "application/json": { "schema": { "type": "object" } } } },
          "default": { "$ref": "#/components/responses/Error" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyBearer": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "kd_ API Key",
        "description": "在 KARDS Decker 个人中心创建。只放在 Authorization 请求头中。"
      }
    },
    "parameters": {
      "Page": {
        "name": "page",
        "in": "query",
        "schema": { "type": "integer", "minimum": 1, "default": 1 }
      },
      "Limit": {
        "name": "limit",
        "in": "query",
        "schema": { "type": "integer", "minimum": 1, "maximum": 50, "default": 20 }
      },
      "UserId": {
        "name": "user_id",
        "in": "path",
        "required": true,
        "schema": { "type": "string", "format": "uuid" },
        "description": "API Key 所属用户的 UUID；路径用户必须与密钥所属用户一致。"
      },
      "DeckId": {
        "name": "deck_id",
        "in": "path",
        "required": true,
        "schema": { "type": "string", "format": "uuid" }
      },
      "CardId": {
        "name": "card_id",
        "in": "path",
        "required": true,
        "schema": { "type": "string" }
      }
    },
    "responses": {
      "Error": {
        "description": "错误响应",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorEnvelope" }
          }
        }
      }
    },
    "schemas": {
      "Faction": {
        "type": "string",
        "enum": ["Germany", "Britain", "Japan", "Soviet", "USA", "France", "Italy", "Poland", "Finland", "Anzac", "Neutral"]
      },
      "PaginationMeta": {
        "type": "object",
        "required": ["page", "limit", "total", "total_pages"],
        "properties": {
          "page": { "type": "integer" },
          "limit": { "type": "integer" },
          "total": { "type": "integer" },
          "total_pages": { "type": "integer" }
        }
      },
      "VersionMeta": {
        "type": "object",
        "required": ["api_version"],
        "properties": { "api_version": { "type": "string", "const": "v1" } }
      },
      "DiscoveryEnvelope": {
        "type": "object",
        "required": ["data", "meta"],
        "properties": {
          "data": {
            "type": "object",
            "required": ["version", "documentation", "endpoints"],
            "properties": {
              "version": { "type": "string", "const": "v1" },
              "documentation": { "type": "string" },
              "endpoints": { "type": "object", "additionalProperties": { "type": "string" } }
            }
          },
          "meta": { "$ref": "#/components/schemas/VersionMeta" }
        }
      },
      "Card": {
        "type": "object",
        "required": ["card_id", "host_factions", "is_exile", "attributes", "pool", "is_reserved", "is_derived"],
        "properties": {
          "card_id": { "type": "string" },
          "import_id": { "type": ["string", "null"] },
          "name_zh": { "type": ["string", "null"] },
          "name_en": { "type": ["string", "null"] },
          "description": { "type": "string" },
          "faction": { "$ref": "#/components/schemas/Faction" },
          "host_factions": { "type": "array", "items": { "$ref": "#/components/schemas/Faction" } },
          "is_exile": { "type": "boolean" },
          "type": { "type": ["string", "null"] },
          "rarity": { "type": ["string", "null"] },
          "kredits": { "type": ["number", "null"] },
          "operation_cost": { "type": ["number", "null"] },
          "attack": { "type": ["number", "null"] },
          "defense": { "type": ["number", "null"] },
          "attributes": { "type": "array", "items": {} },
          "set": { "type": ["string", "null"] },
          "pool": { "type": "string", "enum": ["active", "reserve", "derived"] },
          "is_reserved": { "type": "boolean" },
          "is_derived": { "type": "boolean" },
          "image_url": { "type": ["string", "null"], "format": "uri" },
          "image_proxy_url": { "type": ["string", "null"], "format": "uri" }
        }
      },
      "CardBack": {
        "type": "object",
        "required": ["id", "name", "image_url"],
        "properties": {
          "id": { "type": "string", "description": "可直接作为 card_back_id 的精确资源路径" },
          "name": { "type": "string" },
          "image_url": { "type": "string", "format": "uri" }
        }
      },
      "DeckGuide": {
        "type": "object",
        "required": ["system"],
        "properties": {
          "schema_version": { "type": "integer", "const": 1, "readOnly": true },
          "prefix": { "type": ["string", "null"], "maxLength": 24 },
          "system": { "type": "string", "maxLength": 40 },
          "variant": { "type": ["string", "null"], "maxLength": 40 },
          "description": { "type": ["string", "null"], "maxLength": 1200, "writeOnly": true },
          "core_cards": {
            "type": "array",
            "maxItems": 30,
            "uniqueItems": true,
            "items": { "type": "string" },
            "description": "必须属于当前卡组的非衍生 card_id"
          },
          "reserve_recommendations": {
            "type": "array",
            "maxItems": 30,
            "uniqueItems": true,
            "items": { "type": "string" },
            "description": "备卡；必须是已知非衍生 card_id，不要求属于当前卡组"
          },
          "substitutions": {
            "type": "array",
            "maxItems": 20,
            "items": {
              "type": "object",
              "required": ["replacement_card_id", "standard_card_id"],
              "properties": {
                "replacement_card_id": { "type": "string" },
                "standard_card_id": { "type": "string" }
              }
            }
          },
          "otk_components": {
            "type": "array",
            "maxItems": 10,
            "items": {
              "type": "array",
              "minItems": 1,
              "maxItems": 10,
              "items": { "type": "string" }
            }
          }
        }
      },
      "DeckArchive": {
        "type": "object",
        "properties": {
          "key_components": { "type": ["object", "null"], "additionalProperties": true },
          "replacements": { "type": ["object", "null"], "additionalProperties": true },
          "chart_settings": { "type": ["object", "null"], "additionalProperties": true }
        }
      },
      "Deck": {
        "type": "object",
        "required": ["id", "name", "main_faction", "deck_code", "cards_snapshot", "tags", "display", "archive"],
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "name": { "type": "string" },
          "description": { "type": ["string", "null"] },
          "main_faction": { "$ref": "#/components/schemas/Faction" },
          "ally_faction": { "oneOf": [{ "$ref": "#/components/schemas/Faction" }, { "type": "null" }] },
          "deck_code": { "type": "string" },
          "cards_snapshot": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["card_id", "count"],
              "properties": {
                "card_id": { "type": "string" },
                "count": { "type": "integer", "minimum": 1, "maximum": 4 }
              }
            }
          },
          "tags": { "type": "array", "items": { "type": "string" }, "maxItems": 12 },
          "share_slug": { "type": ["string", "null"] },
          "view_count": { "type": "integer" },
          "like_count": { "type": "integer" },
          "display": {
            "type": "object",
            "properties": {
              "featured_card_id": { "type": ["string", "null"] },
              "card_back_id": { "type": ["string", "null"] }
            }
          },
          "guide": { "oneOf": [{ "$ref": "#/components/schemas/DeckGuide" }, { "type": "null" }] },
          "archive": { "$ref": "#/components/schemas/DeckArchive" },
          "visibility": { "type": "string", "enum": ["public", "private", "unlisted"] },
          "sort_order": { "type": "integer" },
          "created_at": { "type": ["string", "null"], "format": "date-time" },
          "updated_at": { "type": ["string", "null"], "format": "date-time" },
          "author": { "type": "object", "additionalProperties": true }
        }
      },
      "CreateDeckInput": {
        "type": "object",
        "required": ["deck_code", "guide"],
        "properties": {
          "deck_code": { "type": "string" },
          "guide": { "$ref": "#/components/schemas/DeckGuide" },
          "archive": { "$ref": "#/components/schemas/DeckArchive" },
          "tags": { "type": "array", "maxItems": 12, "items": { "type": "string", "maxLength": 20 } },
          "visibility": { "type": "string", "enum": ["public", "private", "unlisted"], "default": "private" },
          "featured_card_id": { "type": ["string", "null"] },
          "card_back_id": { "type": ["string", "null"] }
        }
      },
      "UpdateDeckInput": {
        "type": "object",
        "minProperties": 1,
        "properties": {
          "deck_code": { "type": "string" },
          "guide": { "$ref": "#/components/schemas/DeckGuide" },
          "archive": { "$ref": "#/components/schemas/DeckArchive" },
          "tags": { "type": "array", "maxItems": 12, "items": { "type": "string", "maxLength": 20 } },
          "visibility": { "type": "string", "enum": ["public", "private", "unlisted"] },
          "featured_card_id": { "type": ["string", "null"] },
          "card_back_id": { "type": ["string", "null"] }
        }
      },
      "ReplaceDeckInput": {
        "type": "object",
        "required": ["deck_code", "replacements"],
        "properties": {
          "deck_code": { "type": "string" },
          "replacements": {
            "type": "array",
            "minItems": 1,
            "maxItems": 20,
            "items": {
              "type": "object",
              "required": ["remove_card_id", "add_card_id", "count"],
              "properties": {
                "remove_card_id": { "type": "string" },
                "add_card_id": { "type": "string" },
                "count": { "type": "integer", "minimum": 1, "maximum": 4 }
              }
            }
          }
        }
      },
      "CardEnvelope": {
        "type": "object",
        "required": ["data", "meta"],
        "properties": { "data": { "$ref": "#/components/schemas/Card" }, "meta": { "$ref": "#/components/schemas/VersionMeta" } }
      },
      "CardListEnvelope": {
        "type": "object",
        "required": ["data", "meta"],
        "properties": { "data": { "type": "array", "items": { "$ref": "#/components/schemas/Card" } }, "meta": { "$ref": "#/components/schemas/PaginationMeta" } }
      },
      "CardBackListEnvelope": {
        "type": "object",
        "required": ["data", "meta"],
        "properties": { "data": { "type": "array", "items": { "$ref": "#/components/schemas/CardBack" } }, "meta": { "$ref": "#/components/schemas/PaginationMeta" } }
      },
      "DeckEnvelope": {
        "type": "object",
        "required": ["data", "meta"],
        "properties": { "data": { "$ref": "#/components/schemas/Deck" }, "meta": { "$ref": "#/components/schemas/VersionMeta" } }
      },
      "DeckListEnvelope": {
        "type": "object",
        "required": ["data", "meta"],
        "properties": { "data": { "type": "array", "items": { "$ref": "#/components/schemas/Deck" } }, "meta": { "$ref": "#/components/schemas/PaginationMeta" } }
      },
      "DeleteEnvelope": {
        "type": "object",
        "required": ["data", "meta"],
        "properties": {
          "data": {
            "type": "object",
            "required": ["id", "deleted"],
            "properties": { "id": { "type": "string", "format": "uuid" }, "deleted": { "type": "boolean", "const": true } }
          },
          "meta": { "$ref": "#/components/schemas/VersionMeta" }
        }
      },
      "ErrorEnvelope": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "object",
            "required": ["code", "message"],
            "properties": {
              "code": { "type": "string" },
              "message": { "type": "string" },
              "details": {}
            }
          }
        }
      }
    }
  }
}
